EPICS Multi-Core Utilities  1.2.3-SNAPSHOT
Real-Time Utilities for EPICS IOCs on Multi-Core Linux
utils.c
Go to the documentation of this file.
1 /********************************************/
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <sched.h>
15 #include <string.h>
16 
17 #include <errlog.h>
18 #include <shareLib.h>
19 
21 #define epicsExportSharedSymbols
23 #include "utils.h"
24 
25 epicsShareDef int cpuDigits;
26 
33 void strToCpuset(cpu_set_t *cpuset, const char *spec)
34 {
35  char *buff = strdup(spec);
36  char *tok, *save = NULL;
37 
38  CPU_ZERO(cpuset);
39 
40  tok = strtok_r(buff, ",", &save);
41  while (tok) {
42  int i;
43  int from, to;
44  from = to = atoi(tok);
45  char *sep = strstr(tok, "-");
46  if (sep) {
47  to = atoi(sep+1);
48  }
49  for (i = from; i <= to; i++) {
50  CPU_SET(i, cpuset);
51  }
52  tok = strtok_r(NULL, ",", &save);
53  }
54 }
55 
63 void cpusetToStr(char *set, size_t len, const cpu_set_t *cpuset)
64 {
65  int cpu = 0;
66  int l;
67  char buf[cpuDigits*2+3];
68 
69  if (!set || !len) return;
70  set[0] = '\0';
71  while (cpu < NO_OF_CPUS) {
72  int from, to;
73  while (!CPU_ISSET(cpu, cpuset) && cpu < NO_OF_CPUS) {
74  cpu++;
75  }
76  if (cpu >= NO_OF_CPUS) {
77  break;
78  }
79  from = to = cpu++;
80  while (CPU_ISSET(cpu, cpuset) && cpu < NO_OF_CPUS) {
81  to = cpu++;
82  }
83  if (from == to) {
84  sprintf(buf, "%d,", from);
85  } else {
86  sprintf(buf, "%d-%d,", from, to);
87  }
88  strncat(set, buf, (len - 1 - strlen(set)));
89  }
90  if ((l = strlen(set))) {
91  set[l-1] = '\0';
92  }
93 }
94 
101 const char *policyToStr(const int policy)
102 {
103  switch (policy) {
104  case SCHED_OTHER:
105  return "OTHER";
106  case SCHED_FIFO:
107  return "FIFO";
108  case SCHED_RR:
109  return "RR";
110 #ifdef SCHED_BATCH
111  case SCHED_BATCH:
112  return "BATCH";
113 #endif /* SCHED_BATCH */
114 #ifdef SCHED_IDLE
115  case SCHED_IDLE:
116  return "IDLE";
117 #endif /* SCHED_IDLE */
118  default:
119  return "?";
120  }
121 }
122 
129 int strToPolicy(const char *string)
130 {
131  int policy = -1;
132  if (string == strcasestr(string, "SCHED_")) {
133  string += 6;
134  }
135  if (0 == strncasecmp(string, "OTHER", 1)) {
136  policy = SCHED_OTHER;
137  } else if (0 == strncasecmp(string, "FIFO", 1)) {
138  policy = SCHED_FIFO;
139  } else if (0 == strncasecmp(string, "RR", 1)) {
140  policy = SCHED_RR;
141  }
142 #ifdef SCHED_BATCH
143  else if (0 == strncasecmp(string, "BATCH", 1)) {
144  policy = SCHED_BATCH;
145  }
146 #endif /* SCHED_BATCH */
147 #ifdef SCHED_IDLE
148  else if (0 == strncasecmp(string, "IDLE", 1)) {
149  policy = SCHED_IDLE;
150  }
151 #endif /* SCHED_IDLE */
152  else {
153  errlogPrintf("Invalid policy \"%s\"\n", string);
154  return -1;
155  }
156  return policy;
157 }
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
void strToCpuset(cpu_set_t *cpuset, const char *spec)
Convert a cpuset string specification (e.g. "0,2-3") to a cpuset.
Definition: utils.c:33
int strToPolicy(const char *string)
Convert string policy specification to policy.
Definition: utils.c:129
Header file for utils.c.
#define NO_OF_CPUS
Definition: utils.h:22