libcpuid_util.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2008 Veselin Georgiev,
  3. * anrieffNOSPAM @ mgail_DOT.com (convert to gmail)
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stdarg.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include "libcpuid.h"
  32. #include "libcpuid_util.h"
  33. int _current_verboselevel;
  34. void match_features(const struct feature_map_t* matchtable, int count, uint32_t reg, struct cpu_id_t* data)
  35. {
  36. int i;
  37. for (i = 0; i < count; i++)
  38. if (reg & (1 << matchtable[i].bit))
  39. data->flags[matchtable[i].feature] = 1;
  40. }
  41. static void default_warn(const char *msg)
  42. {
  43. fprintf(stderr, "%s", msg);
  44. }
  45. libcpuid_warn_fn_t _warn_fun = default_warn;
  46. #if defined(_MSC_VER)
  47. # define vsnprintf _vsnprintf
  48. #endif
  49. void warnf(const char* format, ...)
  50. {
  51. char buff[1024];
  52. va_list va;
  53. if (!_warn_fun) return;
  54. va_start(va, format);
  55. vsnprintf(buff, sizeof(buff), format, va);
  56. va_end(va);
  57. _warn_fun(buff);
  58. }
  59. void debugf(int verboselevel, const char* format, ...)
  60. {
  61. char buff[1024];
  62. va_list va;
  63. if (verboselevel > _current_verboselevel) return;
  64. va_start(va, format);
  65. vsnprintf(buff, sizeof(buff), format, va);
  66. va_end(va);
  67. _warn_fun(buff);
  68. }
  69. static int score(const struct match_entry_t* entry, const struct cpu_id_t* data,
  70. int brand_code, int model_code)
  71. {
  72. int res = 0;
  73. if (entry->family == data->family ) res++;
  74. if (entry->model == data->model ) res++;
  75. if (entry->stepping == data->stepping ) res++;
  76. if (entry->ext_family == data->ext_family) res++;
  77. if (entry->ext_model == data->ext_model ) res++;
  78. if (entry->ncores == data->num_cores ) res++;
  79. if (entry->l2cache == data->l2_cache ) res++;
  80. if (entry->l3cache == data->l3_cache ) res++;
  81. if (entry->brand_code == brand_code ) res++;
  82. if (entry->model_code == model_code ) res++;
  83. return res;
  84. }
  85. void match_cpu_codename(const struct match_entry_t* matchtable, int count,
  86. struct cpu_id_t* data, int brand_code, int model_code)
  87. {
  88. int bestscore = -1;
  89. int bestindex = 0;
  90. int i, t;
  91. debugf(3, "Matching cpu f:%d, m:%d, s:%d, xf:%d, xm:%d, ncore:%d, l2:%d, bcode:%d, code:%d\n",
  92. data->family, data->model, data->stepping, data->ext_family,
  93. data->ext_model, data->num_cores, data->l2_cache, brand_code, model_code);
  94. for (i = 0; i < count; i++) {
  95. t = score(&matchtable[i], data, brand_code, model_code);
  96. debugf(3, "Entry %d, `%s', score %d\n", i, matchtable[i].name, t);
  97. if (t > bestscore) {
  98. debugf(2, "Entry `%s' selected - best score so far (%d)\n", matchtable[i].name, t);
  99. bestscore = t;
  100. bestindex = i;
  101. }
  102. }
  103. strcpy(data->cpu_codename, matchtable[bestindex].name);
  104. }
  105. void generic_get_cpu_list(const struct match_entry_t* matchtable, int count,
  106. struct cpu_list_t* list)
  107. {
  108. int i, j, n, good;
  109. n = 0;
  110. list->names = (char**) malloc(sizeof(char*) * count);
  111. for (i = 0; i < count; i++) {
  112. if (strstr(matchtable[i].name, "Unknown")) continue;
  113. good = 1;
  114. for (j = n - 1; j >= 0; j--)
  115. if (!strcmp(list->names[j], matchtable[i].name)) {
  116. good = 0;
  117. break;
  118. }
  119. if (!good) continue;
  120. list->names[n++] = strdup(matchtable[i].name);
  121. }
  122. list->num_entries = n;
  123. }
  124. static int xmatch_entry(char c, const char* p)
  125. {
  126. int i, j;
  127. if (c == 0) return -1;
  128. if (c == p[0]) return 1;
  129. if (p[0] == '.') return 1;
  130. if (p[0] == '#' && isdigit(c)) return 1;
  131. if (p[0] == '[') {
  132. j = 1;
  133. while (p[j] && p[j] != ']') j++;
  134. if (!p[j]) return -1;
  135. for (i = 1; i < j; i++)
  136. if (p[i] == c) return j + 1;
  137. }
  138. return -1;
  139. }
  140. int match_pattern(const char* s, const char* p)
  141. {
  142. int i, j, dj, k, n, m;
  143. n = (int) strlen(s);
  144. m = (int) strlen(p);
  145. for (i = 0; i < n; i++) {
  146. if (xmatch_entry(s[i], p) != -1) {
  147. j = 0;
  148. k = 0;
  149. while (j < m && ((dj = xmatch_entry(s[i + k], p + j)) != -1)) {
  150. k++;
  151. j += dj;
  152. }
  153. if (j == m) return i + 1;
  154. }
  155. }
  156. return 0;
  157. }
  158. struct cpu_id_t* get_cached_cpuid(void)
  159. {
  160. static int initialized = 0;
  161. static struct cpu_id_t id;
  162. if (initialized) return &id;
  163. if (cpu_identify(NULL, &id))
  164. memset(&id, 0, sizeof(id));
  165. initialized = 1;
  166. return &id;
  167. }