utils.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/types.h>
  15. #include <sys/utsname.h>
  16. #include <unistd.h>
  17. #include "utils.h"
  18. static char auxv[4096];
  19. int read_auxv(char *buf, ssize_t buf_size)
  20. {
  21. ssize_t num;
  22. int rc, fd;
  23. fd = open("/proc/self/auxv", O_RDONLY);
  24. if (fd == -1) {
  25. perror("open");
  26. return -errno;
  27. }
  28. num = read(fd, buf, buf_size);
  29. if (num < 0) {
  30. perror("read");
  31. rc = -EIO;
  32. goto out;
  33. }
  34. if (num > buf_size) {
  35. printf("overflowed auxv buffer\n");
  36. rc = -EOVERFLOW;
  37. goto out;
  38. }
  39. rc = 0;
  40. out:
  41. close(fd);
  42. return rc;
  43. }
  44. void *find_auxv_entry(int type, char *auxv)
  45. {
  46. ElfW(auxv_t) *p;
  47. p = (ElfW(auxv_t) *)auxv;
  48. while (p->a_type != AT_NULL) {
  49. if (p->a_type == type)
  50. return p;
  51. p++;
  52. }
  53. return NULL;
  54. }
  55. void *get_auxv_entry(int type)
  56. {
  57. ElfW(auxv_t) *p;
  58. if (read_auxv(auxv, sizeof(auxv)))
  59. return NULL;
  60. p = find_auxv_entry(type, auxv);
  61. if (p)
  62. return (void *)p->a_un.a_val;
  63. return NULL;
  64. }
  65. int pick_online_cpu(void)
  66. {
  67. cpu_set_t mask;
  68. int cpu;
  69. CPU_ZERO(&mask);
  70. if (sched_getaffinity(0, sizeof(mask), &mask)) {
  71. perror("sched_getaffinity");
  72. return -1;
  73. }
  74. /* We prefer a primary thread, but skip 0 */
  75. for (cpu = 8; cpu < CPU_SETSIZE; cpu += 8)
  76. if (CPU_ISSET(cpu, &mask))
  77. return cpu;
  78. /* Search for anything, but in reverse */
  79. for (cpu = CPU_SETSIZE - 1; cpu >= 0; cpu--)
  80. if (CPU_ISSET(cpu, &mask))
  81. return cpu;
  82. printf("No cpus in affinity mask?!\n");
  83. return -1;
  84. }
  85. bool is_ppc64le(void)
  86. {
  87. struct utsname uts;
  88. int rc;
  89. errno = 0;
  90. rc = uname(&uts);
  91. if (rc) {
  92. perror("uname");
  93. return false;
  94. }
  95. return strcmp(uts.machine, "ppc64le") == 0;
  96. }