utils.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. void *get_auxv_entry(int type)
  18. {
  19. ElfW(auxv_t) *p;
  20. void *result;
  21. ssize_t num;
  22. int fd;
  23. fd = open("/proc/self/auxv", O_RDONLY);
  24. if (fd == -1) {
  25. perror("open");
  26. return NULL;
  27. }
  28. result = NULL;
  29. num = read(fd, auxv, sizeof(auxv));
  30. if (num < 0) {
  31. perror("read");
  32. goto out;
  33. }
  34. if (num > sizeof(auxv)) {
  35. printf("Overflowed auxv buffer\n");
  36. goto out;
  37. }
  38. p = (ElfW(auxv_t) *)auxv;
  39. while (p->a_type != AT_NULL) {
  40. if (p->a_type == type) {
  41. result = (void *)p->a_un.a_val;
  42. break;
  43. }
  44. p++;
  45. }
  46. out:
  47. close(fd);
  48. return result;
  49. }
  50. int pick_online_cpu(void)
  51. {
  52. cpu_set_t mask;
  53. int cpu;
  54. CPU_ZERO(&mask);
  55. if (sched_getaffinity(0, sizeof(mask), &mask)) {
  56. perror("sched_getaffinity");
  57. return -1;
  58. }
  59. /* We prefer a primary thread, but skip 0 */
  60. for (cpu = 8; cpu < CPU_SETSIZE; cpu += 8)
  61. if (CPU_ISSET(cpu, &mask))
  62. return cpu;
  63. /* Search for anything, but in reverse */
  64. for (cpu = CPU_SETSIZE - 1; cpu >= 0; cpu--)
  65. if (CPU_ISSET(cpu, &mask))
  66. return cpu;
  67. printf("No cpus in affinity mask?!\n");
  68. return -1;
  69. }