Skip to content

Commit

Permalink
mx_util: Add functions to scan and format cpusets
Browse files Browse the repository at this point in the history
  • Loading branch information
donald authored and mariux committed Oct 16, 2015
1 parent 11dacb9 commit e6c3e31
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
103 changes: 103 additions & 0 deletions mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,66 @@ char **mx_strvec_from_str(char *str)
return strvec;
}

int mx_str_to_cpuset(cpu_set_t* cpuset_ptr,char *str)
{
char c;
int cpu_low;
int cpu_high;
char *next;
int i;

CPU_ZERO(cpuset_ptr);

while (1) {
c=*str;
if (c=='\0') {
break;
} else if (c>='0' && c<='9') {
cpu_low=strtol(str,&next,10);
str=next;
} else {
return -(errno=EINVAL);
}

if (cpu_low<0 || cpu_low>=CPU_SETSIZE) {
return -(errno=EINVAL);
}

c=*str;
if (c=='\0') {
CPU_SET(cpu_low,cpuset_ptr);
break;
} else if (c==',') {
CPU_SET(cpu_low,cpuset_ptr);
str++;
} else if (c=='-') {
c=*++str;
if (c>='0' && c<='9') {
cpu_high=strtol(str,&next,10);
str=next;
if (cpu_high<0 || cpu_high>=CPU_SETSIZE || cpu_high<cpu_low) {
return -(errno=EINVAL);
}
for (i=cpu_low;i<=cpu_high;i++) {
CPU_SET(i,cpuset_ptr);
} c=*str++;
if (c=='\0') {
break;
} else if (c==',') {
/* noop */
} else {
return -(errno=EINVAL);
}
} else {
return -(errno=EINVAL);
}
} else {
return -(errno=EINVAL);
}
}
return 0;
}

char *mx_strvec_join(char *sep,char **strvec)
{
int elements=0;
Expand Down Expand Up @@ -1204,3 +1264,46 @@ char *mx_strvec_join(char *sep,char **strvec)
*p='\0';
return(out);
}

char *mx_cpuset_to_str(cpu_set_t* cpuset_ptr)
{
char **strvec;
int cpu;
int cpu_low;
int cpu_high;
char *str;
int res;
char *out;

strvec=mx_strvec_new();
if (!strvec) return NULL;

cpu=0;
while(1) {
if (cpu>=CPU_SETSIZE) break;
if (CPU_ISSET(cpu,cpuset_ptr)) {
cpu_low=cpu++;
while (1) {
if (cpu>=CPU_SETSIZE || !CPU_ISSET(cpu,cpuset_ptr)) break;
cpu++;
}
cpu_high=cpu-1;
if (cpu_low==cpu_high) {
mx_asprintf_forever(&str,"%d",cpu_low);
} else {
mx_asprintf_forever(&str,"%d-%d",cpu_low,cpu_high);
}
res=mx_strvec_push_str(&strvec,str);
if (!res) {
mx_strvec_free(strvec);
return NULL;
}
} else {
cpu++;
}
}

out=mx_strvec_join(",",strvec);
mx_strvec_free(strvec);
return out;
}
4 changes: 4 additions & 0 deletions mx_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <sched.h>

#include "mx_log.h"

Expand Down Expand Up @@ -192,4 +193,7 @@ char** mx_strvec_from_str(char *str);
void mx_strvec_free(char **strvec);
char* mx_strvec_join(char *sep,char **strvec);

char* mx_cpuset_to_str(cpu_set_t* cpuset_ptr);
int mx_str_to_cpuset(cpu_set_t* cpuset_ptr,char *str);

#endif
22 changes: 22 additions & 0 deletions test_mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,27 @@ static void test_mx_strvec() {
mx_strvec_free(strvec);
}

static void test_mx_cpuset(void)
{
cpu_set_t cpuset;
char *str;

assert(mx_str_to_cpuset(&cpuset,"1,2,3,10,11,12,100-102")==0);
assert((str=mx_cpuset_to_str(&cpuset)));
assert(strcmp(str,"1-3,10-12,100-102")==0);
free(str);

assert(mx_str_to_cpuset(&cpuset,"")==0);
assert((str=mx_cpuset_to_str(&cpuset)));
assert(strcmp(str,"")==0);
free(str);

assert(mx_str_to_cpuset(&cpuset,"bla")<0);
assert(mx_str_to_cpuset(&cpuset,"5-4")<0);
assert(mx_str_to_cpuset(&cpuset,"-4")<0);
assert(mx_str_to_cpuset(&cpuset,"4-")<0);
}

int main(int argc, char *argv[])
{
test_mx_strskipwhitespaces();
Expand All @@ -408,5 +429,6 @@ int main(int argc, char *argv[])
test_mx_read_first_line_from_file();
test_mx_strscan();
test_mx_strvec();
test_mx_cpuset();
return 0;
}

0 comments on commit e6c3e31

Please sign in to comment.