alloc_affinity.c 724 B

1234567891011121314151617181920212223242526272829
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 2024 Denys Vlasenko
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. #include <sched.h>
  10. #include "libbb.h"
  11. unsigned long* FAST_FUNC get_malloc_cpu_affinity(int pid, unsigned *sz)
  12. {
  13. unsigned long *mask = NULL;
  14. unsigned sz_in_bytes = *sz;
  15. for (;;) {
  16. mask = xrealloc(mask, sz_in_bytes);
  17. if (sched_getaffinity(pid, sz_in_bytes, (void*)mask) == 0)
  18. break; /* got it */
  19. sz_in_bytes *= 2;
  20. if (errno == EINVAL && (int)sz_in_bytes > 0)
  21. continue;
  22. bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
  23. }
  24. //bb_error_msg("get mask[0]:%lx sz_in_bytes:%d", mask[0], sz_in_bytes);
  25. *sz = sz_in_bytes;
  26. return mask;
  27. }