utils.c 1.6 KB

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