utils.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2013-2015, Michael Ellerman, IBM Corp.
  3. * Licensed under GPLv2.
  4. */
  5. #define _GNU_SOURCE /* For CPU_ZERO etc. */
  6. #include <elf.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <link.h>
  10. #include <sched.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <sys/stat.h>
  14. #include <sys/sysinfo.h>
  15. #include <sys/types.h>
  16. #include <sys/utsname.h>
  17. #include <unistd.h>
  18. #include "utils.h"
  19. static char auxv[4096];
  20. int read_auxv(char *buf, ssize_t buf_size)
  21. {
  22. ssize_t num;
  23. int rc, fd;
  24. fd = open("/proc/self/auxv", O_RDONLY);
  25. if (fd == -1) {
  26. perror("open");
  27. return -errno;
  28. }
  29. num = read(fd, buf, buf_size);
  30. if (num < 0) {
  31. perror("read");
  32. rc = -EIO;
  33. goto out;
  34. }
  35. if (num > buf_size) {
  36. printf("overflowed auxv buffer\n");
  37. rc = -EOVERFLOW;
  38. goto out;
  39. }
  40. rc = 0;
  41. out:
  42. close(fd);
  43. return rc;
  44. }
  45. void *find_auxv_entry(int type, char *auxv)
  46. {
  47. ElfW(auxv_t) *p;
  48. p = (ElfW(auxv_t) *)auxv;
  49. while (p->a_type != AT_NULL) {
  50. if (p->a_type == type)
  51. return p;
  52. p++;
  53. }
  54. return NULL;
  55. }
  56. void *get_auxv_entry(int type)
  57. {
  58. ElfW(auxv_t) *p;
  59. if (read_auxv(auxv, sizeof(auxv)))
  60. return NULL;
  61. p = find_auxv_entry(type, auxv);
  62. if (p)
  63. return (void *)p->a_un.a_val;
  64. return NULL;
  65. }
  66. int pick_online_cpu(void)
  67. {
  68. int ncpus, cpu = -1;
  69. cpu_set_t *mask;
  70. size_t size;
  71. ncpus = get_nprocs_conf();
  72. size = CPU_ALLOC_SIZE(ncpus);
  73. mask = CPU_ALLOC(ncpus);
  74. if (!mask) {
  75. perror("malloc");
  76. return -1;
  77. }
  78. CPU_ZERO_S(size, mask);
  79. if (sched_getaffinity(0, size, mask)) {
  80. perror("sched_getaffinity");
  81. goto done;
  82. }
  83. /* We prefer a primary thread, but skip 0 */
  84. for (cpu = 8; cpu < ncpus; cpu += 8)
  85. if (CPU_ISSET_S(cpu, size, mask))
  86. goto done;
  87. /* Search for anything, but in reverse */
  88. for (cpu = ncpus - 1; cpu >= 0; cpu--)
  89. if (CPU_ISSET_S(cpu, size, mask))
  90. goto done;
  91. printf("No cpus in affinity mask?!\n");
  92. done:
  93. CPU_FREE(mask);
  94. return cpu;
  95. }
  96. bool is_ppc64le(void)
  97. {
  98. struct utsname uts;
  99. int rc;
  100. errno = 0;
  101. rc = uname(&uts);
  102. if (rc) {
  103. perror("uname");
  104. return false;
  105. }
  106. return strcmp(uts.machine, "ppc64le") == 0;
  107. }