关键词搜索

源码搜索 ×
×

漫话Redis源码之二十

发布2021-11-28浏览1029次

详情内容

这个函数用于在cpu list中设置cpu的affinity:

  1. /* set current thread cpu affinity to cpu list, this function works like
  2. * taskset command (actually cpulist parsing logic reference to util-linux).
  3. * example of this function: "0,2,3", "0,2-3", "0-20:2". */
  4. void setcpuaffinity(const char *cpulist) {
  5. const char *p, *q;
  6. char *end = NULL;
  7. #ifdef __linux__
  8. cpu_set_t cpuset;
  9. #endif
  10. #if defined (__FreeBSD__) || defined(__DragonFly__)
  11. cpuset_t cpuset;
  12. #endif
  13. #ifdef __NetBSD__
  14. cpuset_t *cpuset;
  15. #endif
  16. if (!cpulist)
  17. return;
  18. #ifndef __NetBSD__
  19. CPU_ZERO(&cpuset);
  20. #else
  21. cpuset = cpuset_create();
  22. #endif
  23. q = cpulist;
  24. while (p = q, q = next_token(q, ','), p) {
  25. int a, b, s;
  26. const char *c1, *c2;
  27. if (next_num(p, &end, &a) != 0)
  28. return;
  29. b = a;
  30. s = 1;
  31. p = end;
  32. c1 = next_token(p, '-');
  33. c2 = next_token(p, ',');
  34. if (c1 != NULL && (c2 == NULL || c1 < c2)) {
  35. if (next_num(c1, &end, &b) != 0)
  36. return;
  37. c1 = end && *end ? next_token(end, ':') : NULL;
  38. if (c1 != NULL && (c2 == NULL || c1 < c2)) {
  39. if (next_num(c1, &end, &s) != 0)
  40. return;
  41. if (s == 0)
  42. return;
  43. }
  44. }
  45. if ((a > b))
  46. return;
  47. while (a <= b) {
  48. #ifndef __NetBSD__
  49. CPU_SET(a, &cpuset);
  50. #else
  51. cpuset_set(a, cpuset);
  52. #endif
  53. a += s;
  54. }
  55. }
  56. if (end && *end)
  57. return;
  58. #ifdef __linux__
  59. sched_setaffinity(0, sizeof(cpuset), &cpuset);
  60. #endif
  61. #ifdef __FreeBSD__
  62. cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset), &cpuset);
  63. #endif
  64. #ifdef __DragonFly__
  65. pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
  66. #endif
  67. #ifdef __NetBSD__
  68. pthread_setaffinity_np(pthread_self(), cpuset_size(cpuset), cpuset);
  69. cpuset_destroy(cpuset);
  70. #endif
  71. }
  72. #endif /* USE_SETCPUAFFINITY */

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载