util.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PERF_UTIL_H
  3. #define __PERF_UTIL_H
  4. #define _BSD_SOURCE 1
  5. /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */
  6. #define _DEFAULT_SOURCE 1
  7. #include <dirent.h>
  8. #include <fcntl.h>
  9. #include <stdbool.h>
  10. #include <stddef.h>
  11. #include <linux/compiler.h>
  12. #include <linux/bitmap.h>
  13. #include <sys/types.h>
  14. #ifndef __cplusplus
  15. #include <internal/cpumap.h>
  16. #endif
  17. extern const char perf_usage_string[];
  18. extern const char perf_more_info_string[];
  19. extern const char *input_name;
  20. /* This will control if perf_{host,guest} will set attr.exclude_{host,guest}. */
  21. extern bool exclude_GH_default;
  22. extern bool perf_host;
  23. extern bool perf_guest;
  24. /* General helper functions */
  25. void usage(const char *err) __noreturn;
  26. void die(const char *err, ...) __noreturn __printf(1, 2);
  27. struct dirent;
  28. struct strlist;
  29. int mkdir_p(char *path, mode_t mode);
  30. int rm_rf(const char *path);
  31. int rm_rf_perf_data(const char *path);
  32. struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *));
  33. bool lsdir_no_dot_filter(const char *name, struct dirent *d);
  34. size_t hex_width(u64 v);
  35. int sysctl__max_stack(void);
  36. bool sysctl__nmi_watchdog_enabled(void);
  37. int perf_tip(char **strp, const char *dirpath);
  38. void cpumask_to_cpulist(char *cpumask, char *cpulist);
  39. void print_separator2(int pre_dash_cnt, const char *s, int post_dash_cnt);
  40. #ifndef HAVE_SCHED_GETCPU_SUPPORT
  41. int sched_getcpu(void);
  42. #endif
  43. #ifndef HAVE_SCANDIRAT_SUPPORT
  44. int scandirat(int dirfd, const char *dirp,
  45. struct dirent ***namelist,
  46. int (*filter)(const struct dirent *),
  47. int (*compar)(const struct dirent **, const struct dirent **));
  48. #endif
  49. extern bool perf_singlethreaded;
  50. void perf_set_singlethreaded(void);
  51. void perf_set_multithreaded(void);
  52. char *perf_exe(char *buf, int len);
  53. #ifndef O_CLOEXEC
  54. #ifdef __sparc__
  55. #define O_CLOEXEC 0x400000
  56. #elif defined(__alpha__) || defined(__hppa__)
  57. #define O_CLOEXEC 010000000
  58. #else
  59. #define O_CLOEXEC 02000000
  60. #endif
  61. #endif
  62. struct perf_debuginfod {
  63. const char *urls;
  64. bool set;
  65. };
  66. void perf_debuginfod_setup(struct perf_debuginfod *di);
  67. char *filename_with_chroot(int pid, const char *filename);
  68. int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x,
  69. size_t msz, const void *init_val);
  70. #define realloc_array_as_needed(a, n, x, v) ({ \
  71. typeof(x) __x = (x); \
  72. __x >= (n) ? \
  73. do_realloc_array_as_needed((void **)&(a), \
  74. &(n), \
  75. __x, \
  76. sizeof(*(a)), \
  77. (const void *)(v)) : \
  78. 0; \
  79. })
  80. static inline bool host_is_bigendian(void)
  81. {
  82. #ifdef __BYTE_ORDER__
  83. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  84. return false;
  85. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  86. return true;
  87. #else
  88. #error "Unrecognized __BYTE_ORDER__"
  89. #endif
  90. #else /* !__BYTE_ORDER__ */
  91. unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
  92. unsigned int *ptr;
  93. ptr = (unsigned int *)(void *)str;
  94. return *ptr == 0x01020304;
  95. #endif
  96. }
  97. #endif /* __PERF_UTIL_H */