proc_cpuinfo.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define CPUINFO_FILE "/proc/cpuinfo"
  5. #define BUFFSIZE 2048
  6. /* vendor_id, model_name size reference Linux kernel struct cpuinfo_x86
  7. * (see Linux's arch/x86/include/asm/processor.h) */
  8. struct cpuinfo {
  9. int processor;
  10. char vendor_id[16];
  11. int cpu_family;
  12. int model;
  13. char model_name[64];
  14. int stepping;
  15. int core_id;
  16. int cpu_cores;
  17. };
  18. static void init_cpuinfo(struct cpuinfo* ci) {
  19. ci->processor = -1;
  20. memset(&ci->vendor_id, 0, sizeof(ci->vendor_id));
  21. ci->cpu_family = -1;
  22. ci->model = -1;
  23. memset(&ci->model_name, 0, sizeof(ci->model_name));
  24. ci->stepping = -1;
  25. ci->core_id = -1;
  26. ci->cpu_cores = -1;
  27. }
  28. static int parse_line(char* line, struct cpuinfo* ci) {
  29. char *k, *v, *p;
  30. if ((p = strchr(line, ':')) == NULL)
  31. goto fmt_err;
  32. /* if the line does not have value string, p[1] should be '\n', otherwise
  33. * p[1] should be ' ' */
  34. if (p[1] == '\n' && !p[2])
  35. return 0; /* No value string */
  36. /* ':' should always be followed by a space */
  37. if (p[1] != ' ')
  38. goto fmt_err;
  39. /* skip ": " to get value string */
  40. v = p + 2;
  41. /* get key string */
  42. *p = '\0';
  43. if ((p = strchr(line, '\t')) != NULL)
  44. *p = '\0';
  45. k = line;
  46. if (!strcmp(k, "processor")) {
  47. sscanf(v, "%d\n", &ci->processor);
  48. } else if (!strcmp(k, "cpu family")) {
  49. sscanf(v, "%d\n", &ci->cpu_family);
  50. } else if (!strcmp(k, "model")) {
  51. sscanf(v, "%d\n", &ci->model);
  52. } else if (!strcmp(k, "stepping")) {
  53. sscanf(v, "%d\n", &ci->stepping);
  54. } else if (!strcmp(k, "core id")) {
  55. sscanf(v, "%d\n", &ci->core_id);
  56. } else if (!strcmp(k, "cpu cores")) {
  57. sscanf(v, "%d\n", &ci->cpu_cores);
  58. } else if (!strcmp(k, "vendor_id")) {
  59. snprintf(ci->vendor_id, sizeof(ci->vendor_id), "%s", v);
  60. } else if (!strcmp(k, "model name")) {
  61. snprintf(ci->model_name, sizeof(ci->model_name), "%s", v);
  62. }
  63. return 0;
  64. fmt_err:
  65. fprintf(stderr, "format error in line: %s\n", line);
  66. return -1;
  67. };
  68. static int check_cpuinfo(struct cpuinfo* ci) {
  69. if (ci->processor == -1) {
  70. fprintf(stderr, "Could not get cpu index\n");
  71. return -1;
  72. }
  73. if (ci->core_id == -1) {
  74. fprintf(stderr, "Could not get core id\n");
  75. return -1;
  76. }
  77. if (ci->cpu_cores == -1) {
  78. fprintf(stderr, "Could not get cpu cores\n");
  79. return -1;
  80. }
  81. return 0;
  82. }
  83. int main(int argc, char* argv[]) {
  84. FILE* fp = NULL;
  85. char line[BUFFSIZE];
  86. struct cpuinfo ci;
  87. int cpu_cnt = 0, rv = 0;
  88. init_cpuinfo(&ci);
  89. if ((fp = fopen(CPUINFO_FILE, "r")) == NULL) {
  90. perror("fopen");
  91. return 1;
  92. }
  93. while (fgets(line, sizeof(line), fp) != NULL) {
  94. if (line[0] == '\n') {
  95. if ((rv = check_cpuinfo(&ci)) != 0)
  96. break;
  97. cpu_cnt++;
  98. init_cpuinfo(&ci);
  99. continue;
  100. }
  101. if ((rv = parse_line(line, &ci)) != 0)
  102. break;
  103. }
  104. fclose(fp);
  105. if (rv != 0)
  106. return 1;
  107. if (cpu_cnt == 0) {
  108. fprintf(stderr, "Could not get online cpu info.\n");
  109. return 1;
  110. }
  111. printf("cpuinfo test passed\n");
  112. return 0;
  113. }