void *test_percpu_spinlock_thread(void *arg)
{ struct spinlock_test_data *data = arg; int i, cpu;
if (rseq_register_current_thread()) {
fprintf(stderr, "Error: rseq_register_current_thread(...) failed(%d): %s\n",
errno, strerror(errno));
abort();
} for (i = 0; i < data->reps; i++) {
cpu = rseq_this_cpu_lock(&data->lock);
data->c[cpu].count++;
rseq_percpu_unlock(&data->lock, cpu);
} if (rseq_unregister_current_thread()) {
fprintf(stderr, "Error: rseq_unregister_current_thread(...) failed(%d): %s\n",
errno, strerror(errno));
abort();
}
return NULL;
}
/* * A simple test which implements a sharded counter using a per-cpu * lock. Obviously real applications might prefer to simply use a * per-cpu increment; however, this is reasonable for a test and the * lock can be extended to synchronize more complicated operations.
*/ void test_percpu_spinlock(void)
{ constint num_threads = 200; int i;
uint64_t sum;
pthread_t test_threads[num_threads]; struct spinlock_test_data data;
memset(&data, 0, sizeof(data));
data.reps = 5000;
for (i = 0; i < num_threads; i++)
pthread_create(&test_threads[i], NULL,
test_percpu_spinlock_thread, &data);
for (i = 0; i < num_threads; i++)
pthread_join(test_threads[i], NULL);
sum = 0; for (i = 0; i < CPU_SETSIZE; i++)
sum += data.c[i].count;
void this_cpu_list_push(struct percpu_list *list, struct percpu_list_node *node, int *_cpu)
{ int cpu;
for (;;) {
intptr_t *targetptr, newval, expect; int ret;
cpu = get_current_cpu_id(); /* Load list->c[cpu].head with single-copy atomicity. */
expect = (intptr_t)RSEQ_READ_ONCE(list->c[cpu].head);
newval = (intptr_t)node;
targetptr = (intptr_t *)&list->c[cpu].head;
node->next = (struct percpu_list_node *)expect;
ret = rseq_cmpeqv_storev(RSEQ_MO_RELAXED, RSEQ_PERCPU,
targetptr, expect, newval, cpu); if (rseq_likely(!ret)) break; /* Retry if comparison fails or rseq aborts. */
} if (_cpu)
*_cpu = cpu;
}
/* * Unlike a traditional lock-less linked list; the availability of a * rseq primitive allows us to implement pop without concerns over * ABA-type races.
*/ struct percpu_list_node *this_cpu_list_pop(struct percpu_list *list, int *_cpu)
{ for (;;) { struct percpu_list_node *head;
intptr_t *targetptr, expectnot, *load; long offset; int ret, cpu;
cpu = get_current_cpu_id();
targetptr = (intptr_t *)&list->c[cpu].head;
expectnot = (intptr_t)NULL;
offset = offsetof(struct percpu_list_node, next);
load = (intptr_t *)&head;
ret = rseq_cmpnev_storeoffp_load(RSEQ_MO_RELAXED, RSEQ_PERCPU,
targetptr, expectnot,
offset, load, cpu); if (rseq_likely(!ret)) { if (_cpu)
*_cpu = cpu; return head;
} if (ret > 0) return NULL; /* Retry if rseq aborts. */
}
}
/* * __percpu_list_pop is not safe against concurrent accesses. Should * only be used on lists that are not concurrently modified.
*/ struct percpu_list_node *__percpu_list_pop(struct percpu_list *list, int cpu)
{ struct percpu_list_node *node;
/* Simultaneous modification to a per-cpu linked list from many threads. */ void test_percpu_list(void)
{ int i, j;
uint64_t sum = 0, expected_sum = 0; struct percpu_list list;
pthread_t test_threads[200];
cpu_set_t allowed_cpus;
memset(&list, 0, sizeof(list));
/* Generate list entries for every usable cpu. */
sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus); for (i = 0; i < CPU_SETSIZE; i++) { if (rseq_use_cpu_index() && !CPU_ISSET(i, &allowed_cpus)) continue; for (j = 1; j <= 100; j++) { struct percpu_list_node *node;
for (i = 0; i < 200; i++)
pthread_create(&test_threads[i], NULL,
test_percpu_list_thread, &list);
for (i = 0; i < 200; i++)
pthread_join(test_threads[i], NULL);
for (i = 0; i < CPU_SETSIZE; i++) { struct percpu_list_node *node;
if (rseq_use_cpu_index() && !CPU_ISSET(i, &allowed_cpus)) continue;
while ((node = __percpu_list_pop(&list, i))) {
sum += node->data;
free(node);
}
}
/* * All entries should now be accounted for (unless some external * actor is interfering with our allowed affinity while this * test is running).
*/
assert(sum == expected_sum);
}
int main(int argc, char **argv)
{ if (rseq_register_current_thread()) {
fprintf(stderr, "Error: rseq_register_current_thread(...) failed(%d): %s\n",
errno, strerror(errno)); goto error;
} if (!rseq_validate_cpu_id()) {
fprintf(stderr, "Error: cpu id getter unavailable\n"); goto error;
}
printf("spinlock\n");
test_percpu_spinlock();
printf("percpu_list\n");
test_percpu_list(); if (rseq_unregister_current_thread()) {
fprintf(stderr, "Error: rseq_unregister_current_thread(...) failed(%d): %s\n",
errno, strerror(errno)); goto error;
} return 0;
error: return -1;
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.