You need to sign in or sign up before continuing.
legacy.c 1.1 KB
Newer Older
M
maweiye 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#include <sys/sysinfo.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define _GNU_SOURCE
#include <dirent.h>

int get_nprocs_conf()
{
	DIR *d = opendir("/sys/devices/system/cpu");
	struct dirent *de;
	unsigned int cnt = 0;

	if (!d)
		return 1;
	while ((de = readdir(d))) {
		if ((de->d_type == DT_DIR) &&
			(strlen(de->d_name) > 3) &&
			(de->d_name[0] == 'c') &&
			(de->d_name[1] == 'p') &&
			(de->d_name[2] == 'u') &&
			(isdigit(de->d_name[3])))
			cnt++;
	}
	closedir(d);
	return cnt;
}

int get_nprocs()
{
	int cnt = 1;
	FILE* fp = fopen("/sys/devices/system/cpu/online", "re");
	if (fp != NULL) {
		char buf[128];
		memset(buf, 0, sizeof(buf));
		if (!fgets(buf, sizeof(buf), fp)) {
			return 1;
		}
		cnt = 0;
		char *tmp, *p;
		tmp = strtok_r(buf, ",", &p);
		while (tmp != NULL) {
			char *tok = strstr(tmp, "-");
			if (tok) {
				tok++;
				cnt += atoi(tok) - atoi(tmp) + 1;
			} else {
				cnt += 1;
			}
			tmp = strtok_r(NULL, ",", &p);
		}
		fclose(fp);
	}
	return cnt;
}

long get_phys_pages()
{
	return sysconf(_SC_PHYS_PAGES);	
}

long get_avphys_pages()
{
	return sysconf(_SC_AVPHYS_PAGES);	
}