proc_cpuinfo.c 3.2 KB

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