EPICS Multi-Core Utilities  1.2.2-SNAPSHOT
Real-Time Utilities for EPICS IOCs on Multi-Core Linux
 All Files Functions Variables Typedefs Macros Groups Pages
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 
19 #include "utils.h"
20 
21 epicsShareDef int cpuDigits;
22 
29 void strToCpuset(cpu_set_t *cpuset, const char *spec)
30 {
31  char *buff = strdup(spec);
32  char *tok, *save = NULL;
33 
34  CPU_ZERO(cpuset);
35 
36  tok = strtok_r(buff, ",", &save);
37  while (tok) {
38  int i;
39  int from, to;
40  from = to = atoi(tok);
41  char *sep = strstr(tok, "-");
42  if (sep) {
43  to = atoi(sep+1);
44  }
45  for (i = from; i <= to; i++) {
46  CPU_SET(i, cpuset);
47  }
48  tok = strtok_r(NULL, ",", &save);
49  }
50 }
51 
59 void cpusetToStr(char *set, size_t len, const cpu_set_t *cpuset)
60 {
61  int cpu = 0;
62  int from, to, l;
63  char buf[cpuDigits*2+3];
64 
65  if (!set || !len) return;
66  set[0] = '\0';
67  while (cpu < NO_OF_CPUS) {
68  while (!CPU_ISSET(cpu, cpuset) && cpu < NO_OF_CPUS) {
69  cpu++;
70  }
71  if (cpu >= NO_OF_CPUS) {
72  break;
73  }
74  from = to = cpu++;
75  while (CPU_ISSET(cpu, cpuset) && cpu < NO_OF_CPUS) {
76  to = cpu++;
77  }
78  if (from == to) {
79  sprintf(buf, "%d,", from);
80  } else {
81  sprintf(buf, "%d-%d,", from, to);
82  }
83  strncat(set, buf, (len - 1 - strlen(set)));
84  }
85  if ((l = strlen(set))) {
86  set[l-1] = '\0';
87  }
88 }
89 
96 const char *policyToStr(const int policy)
97 {
98  switch (policy) {
99  case SCHED_OTHER:
100  return "OTHER";
101  case SCHED_FIFO:
102  return "FIFO";
103  case SCHED_RR:
104  return "RR";
105 #ifdef SCHED_BATCH
106  case SCHED_BATCH:
107  return "BATCH";
108 #endif /* SCHED_BATCH */
109 #ifdef SCHED_IDLE
110  case SCHED_IDLE:
111  return "IDLE";
112 #endif /* SCHED_IDLE */
113  default:
114  return "?";
115  }
116 }
117 
124 int strToPolicy(const char *string)
125 {
126  int policy = -1;
127  if (string == strcasestr(string, "SCHED_")) {
128  string += 6;
129  }
130  if (0 == strncasecmp(string, "OTHER", 1)) {
131  policy = SCHED_OTHER;
132  } else if (0 == strncasecmp(string, "FIFO", 1)) {
133  policy = SCHED_FIFO;
134  } else if (0 == strncasecmp(string, "RR", 1)) {
135  policy = SCHED_RR;
136  }
137 #ifdef SCHED_BATCH
138  else if (0 == strncasecmp(string, "BATCH", 1)) {
139  policy = SCHED_BATCH;
140  }
141 #endif /* SCHED_BATCH */
142 #ifdef SCHED_IDLE
143  else if (0 == strncasecmp(string, "IDLE", 1)) {
144  policy = SCHED_IDLE;
145  }
146 #endif /* SCHED_IDLE */
147  else {
148  errlogPrintf("Invalid policy \"%s\"\n", string);
149  return -1;
150  }
151  return policy;
152 }