EPICS Multi-Core Utilities  1.2.3-SNAPSHOT
Real-Time Utilities for EPICS IOCs on Multi-Core Linux
threadShow.c
Go to the documentation of this file.
1 /********************************************/
19 #include <stdlib.h>
20 #include <sched.h>
21 #include <string.h>
22 #include <pthread.h>
23 
24 #include <ellLib.h>
25 #include <errlog.h>
26 #include <epicsStdio.h>
27 #include <epicsEvent.h>
28 #include <epicsThread.h>
29 #include <epicsMath.h>
30 #include <shareLib.h>
31 
32 #include "utils.h"
33 
35 #define epicsExportSharedSymbols
37 #include "mcoreutils.h"
38 
39 static epicsThreadId showThread;
40 static unsigned int showLevel;
41 static char *buffer;
42 static char *cpuspec;
43 static const char *policies;
44 
51 static void mcoreThreadShowPrint(epicsThreadOSD *pthreadInfo, unsigned int level)
52 {
53  if (!pthreadInfo) {
54  fprintf(epicsGetStdout(), " NAME EPICS ID "
55  "LWP ID OSIPRI OSSPRI STATE POLICY CPUSET\n");
56  } else {
57  struct sched_param param;
58  int priority = 0;
59  int policy;
60 
61  policies = "?";
62  cpuspec[0] = '?'; cpuspec[1] = '\0';
63  if (pthreadInfo->tid) {
64  cpu_set_t cpuset;
65  int status;
66  status = pthread_getschedparam(pthreadInfo->tid,
67  &policy,
68  &param);
69  if (errVerbose)
70  checkStatus(status,"pthread_getschedparam");
71 
72  if (!status) {
73  priority = param.sched_priority;
74  policies = policyToStr(policy);
75  }
76 
77  status = pthread_getaffinity_np(pthreadInfo->tid,
78  sizeof(cpu_set_t),
79  &cpuset);
80  if (!status) {
81  cpusetToStr(cpuspec, NO_OF_CPUS+2, &cpuset);
82  }
83  }
84 
85  fprintf(epicsGetStdout(),"%16.16s %14p %8lu %3d%8d %8.8s %7.7s %s\n",
86  pthreadInfo->name,
87  (void *)pthreadInfo,
88  (unsigned long)pthreadInfo->lwpId,
89  pthreadInfo->osiPriority, priority,
90  pthreadInfo->isSuspended ? "SUSPEND" : "OK",
91  policies, cpuspec);
92  }
93 }
94 
100 static void mcoreThreadInfo(epicsThreadId id)
101 {
102  mcoreThreadShowPrint(id, showLevel);
103 }
104 
111 static void mcoreThreadInfoOne(epicsThreadId id)
112 {
113  intptr_t u = id->lwpId;
114  if (id == showThread || (epicsThreadId) u == showThread) {
115  mcoreThreadInfo(id);
116  }
117 }
118 
122 void mcoreThreadShow(epicsThreadId thread, unsigned int level)
123 {
124  if (!thread) {
125  mcoreThreadShowPrint(0, level);
126  return;
127  }
128  showThread = thread;
129  showLevel = level;
130  epicsThreadMap(mcoreThreadInfoOne);
131 }
132 
136 void mcoreThreadShowAll(unsigned int level)
137 {
138  showLevel = level;
139  mcoreThreadShowPrint(0, level);
140  epicsThreadMap(mcoreThreadInfo);
141 }
142 
143 static void once(void *arg)
144 {
145  cpuDigits = (int) log10(NO_OF_CPUS-1) + 1;
146  if (!buffer) buffer = (char *) calloc(cpuDigits+2, sizeof(char));
147  if (!cpuspec) cpuspec = (char *) calloc(NO_OF_CPUS + 2, sizeof(char));
148  printf("MCoreUtils version " VERSION "\n");
149 }
150 
155 {
156  static epicsThreadOnceId onceFlag = EPICS_THREAD_ONCE_INIT;
157  epicsThreadOnce(&onceFlag, once, NULL);
158 }
159 
void mcoreThreadShowAll(unsigned int level)
Show thread info for all threads.
Definition: threadShow.c:136
void mcoreThreadShow(epicsThreadId thread, unsigned int level)
Show thread info for one thread.
Definition: threadShow.c:122
void mcoreThreadShowInit(void)
Initialization routine.
Definition: threadShow.c:154
const char * policyToStr(const int policy)
Convert scheduling policy to string.
Definition: utils.c:101
void cpusetToStr(char *set, size_t len, const cpu_set_t *cpuset)
Convert a cpuset into its string specification (e.g. "0,2-3").
Definition: utils.c:63
epicsShareDef int cpuDigits
Definition: utils.c:25
Header file for utils.c.
#define checkStatus(status, message)
Definition: utils.h:24
#define NO_OF_CPUS
Definition: utils.h:22