FreeBSD Bugzilla – Attachment 156213 Details for
Bug 198149
[hwpmc] pmcstat -P -t (top mode, process sampling) stops after a while
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
threadspin.c
threadspin.c (text/plain), 2.20 KB, created by
John Baldwin
on 2015-05-01 15:35:31 UTC
(
hide
)
Description:
threadspin.c
Filename:
MIME Type:
Creator:
John Baldwin
Created:
2015-05-01 15:35:31 UTC
Size:
2.20 KB
patch
obsolete
>#include <sys/param.h> >#include <assert.h> >#include <err.h> >#include <math.h> >#include <pthread.h> >#include <pthread_np.h> >#include <stdbool.h> >#include <stdio.h> >#include <stdlib.h> >#include <strings.h> >#include <unistd.h> > >static volatile int dying; >static bool bind_cpus; > >static void >usage(void) >{ > > fprintf(stderr, "Usage: threadspin [-b]\n"); > exit(1); >} > >/* Bind the current thread to the specified CPU. */ >static void >bind_cpu(int cpu) >{ > cpuset_t set; > > CPU_ZERO(&set); > CPU_SET(cpu, &set); > if (pthread_setaffinity_np(pthread_self(), sizeof(set), &set) < 0) > err(1, "pthread_setaffinity_np(%d)", cpu); >} > >static void * >worker(void *arg) >{ > double x; > int cpu; > > cpu = (intptr_t)arg; > if (bind_cpus) > bind_cpu(cpu); > x = (double)random(); > while (!dying) { > x = sqrt(x); > x *= x; > } > pthread_exit(NULL); >} > >int >main(int ac, char **av) >{ > cpuset_t all_cpus; > pthread_t *threads; > int ch, cpu, error, i, ncpu; > > bind_cpus = false; > while ((ch = getopt(ac, av, "b")) != -1) > switch (ch) { > case 'b': > bind_cpus = true; > break; > default: > usage(); > break; > } > > /* > * Find all the CPUs this program is eligible to run on and use > * this as our global set. This means you can use cpuset to > * restrict this program to only run on a subset of CPUs. > */ > if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, > sizeof(all_cpus), &all_cpus) < 0) > err(1, "cpuset_getaffinity"); >#ifdef CPU_COUNT > ncpu = CPU_COUNT(&all_cpus); >#else > for (ncpu = 0, i = 0; i < CPU_SETSIZE; i++) { > if (CPU_ISSET(i, &all_cpus)) > ncpu++; > } >#endif > > printf("main: PID %d\n", getpid()); > printf("main: creating threads\n"); > threads = calloc(ncpu, sizeof(*threads)); > if (threads == NULL) > err(1, "calloc"); > for (i = 0; i < ncpu; i++) { > assert(!CPU_EMPTY(&all_cpus)); > cpu = CPU_FFS(&all_cpus) - 1; > CPU_CLR(cpu, &all_cpus); > error = pthread_create(&threads[i], NULL, worker, > (void *)(uintptr_t)cpu); > if (error) > errc(1, error, "pthread_create"); > } > assert(CPU_EMPTY(&all_cpus)); > > (void)getchar(); > printf("main: killing threads\n"); > dying = 1; > printf("main: joining threads\n"); > for (i = 0; i < ncpu; i++) { > error = pthread_join(threads[i], NULL); > if (error) > errc(1, error, "pthread_join"); > } > > return (0); >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 198149
: 156213 |
156477
|
156483