trace-agent.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Guest agent for virtio-trace
  4. *
  5. * Copyright (C) 2012 Hitachi, Ltd.
  6. * Created by Yoshihiro Yunomae <yoshihiro.yunomae.ez@hitachi.com>
  7. * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
  8. */
  9. #define _GNU_SOURCE
  10. #include <limits.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include "trace-agent.h"
  15. #define PAGE_SIZE (sysconf(_SC_PAGE_SIZE))
  16. #define PIPE_DEF_BUFS 16
  17. #define PIPE_MIN_SIZE (PAGE_SIZE*PIPE_DEF_BUFS)
  18. #define PIPE_MAX_SIZE (1024*1024)
  19. #define TRACEFS "/sys/kernel/tracing"
  20. #define DEBUGFS "/sys/kernel/debug/tracing"
  21. #define READ_PATH_FMT "%s/per_cpu/cpu%d/trace_pipe_raw"
  22. #define WRITE_PATH_FMT "/dev/virtio-ports/trace-path-cpu%d"
  23. #define CTL_PATH "/dev/virtio-ports/agent-ctl-path"
  24. pthread_mutex_t mutex_notify = PTHREAD_MUTEX_INITIALIZER;
  25. pthread_cond_t cond_wakeup = PTHREAD_COND_INITIALIZER;
  26. static int get_total_cpus(void)
  27. {
  28. int nr_cpus = (int)sysconf(_SC_NPROCESSORS_CONF);
  29. if (nr_cpus <= 0) {
  30. pr_err("Could not read cpus\n");
  31. goto error;
  32. } else if (nr_cpus > MAX_CPUS) {
  33. pr_err("Exceed max cpus(%d)\n", (int)MAX_CPUS);
  34. goto error;
  35. }
  36. return nr_cpus;
  37. error:
  38. exit(EXIT_FAILURE);
  39. }
  40. static void *agent_info_new(void)
  41. {
  42. struct agent_info *s;
  43. int i;
  44. s = zalloc(sizeof(struct agent_info));
  45. if (s == NULL) {
  46. pr_err("agent_info zalloc error\n");
  47. exit(EXIT_FAILURE);
  48. }
  49. s->pipe_size = PIPE_INIT;
  50. s->use_stdout = false;
  51. s->cpus = get_total_cpus();
  52. s->ctl_fd = -1;
  53. /* read/write threads init */
  54. for (i = 0; i < s->cpus; i++)
  55. s->rw_ti[i] = rw_thread_info_new();
  56. return s;
  57. }
  58. static unsigned long parse_size(const char *arg)
  59. {
  60. unsigned long value, round;
  61. char *ptr;
  62. value = strtoul(arg, &ptr, 10);
  63. switch (*ptr) {
  64. case 'K': case 'k':
  65. value <<= 10;
  66. break;
  67. case 'M': case 'm':
  68. value <<= 20;
  69. break;
  70. default:
  71. break;
  72. }
  73. if (value > PIPE_MAX_SIZE) {
  74. pr_err("Pipe size must be less than 1MB\n");
  75. goto error;
  76. } else if (value < PIPE_MIN_SIZE) {
  77. pr_err("Pipe size must be over 64KB\n");
  78. goto error;
  79. }
  80. /* Align buffer size with page unit */
  81. round = value & (PAGE_SIZE - 1);
  82. value = value - round;
  83. return value;
  84. error:
  85. return 0;
  86. }
  87. static void usage(char const *prg)
  88. {
  89. pr_err("usage: %s [-h] [-o] [-s <size of pipe>]\n", prg);
  90. }
  91. static const char *make_path(int cpu_num, bool this_is_write_path)
  92. {
  93. int ret;
  94. char *buf;
  95. buf = zalloc(PATH_MAX);
  96. if (buf == NULL) {
  97. pr_err("Could not allocate buffer\n");
  98. goto error;
  99. }
  100. if (this_is_write_path)
  101. /* write(output) path */
  102. ret = snprintf(buf, PATH_MAX, WRITE_PATH_FMT, cpu_num);
  103. else {
  104. /* read(input) path */
  105. ret = snprintf(buf, PATH_MAX, READ_PATH_FMT, TRACEFS, cpu_num);
  106. if (ret > 0 && access(buf, F_OK) != 0)
  107. ret = snprintf(buf, PATH_MAX, READ_PATH_FMT, DEBUGFS, cpu_num);
  108. }
  109. if (ret <= 0) {
  110. pr_err("Failed to generate %s path(CPU#%d):%d\n",
  111. this_is_write_path ? "read" : "write", cpu_num, ret);
  112. goto error;
  113. }
  114. return buf;
  115. error:
  116. free(buf);
  117. return NULL;
  118. }
  119. static const char *make_input_path(int cpu_num)
  120. {
  121. return make_path(cpu_num, false);
  122. }
  123. static const char *make_output_path(int cpu_num)
  124. {
  125. return make_path(cpu_num, true);
  126. }
  127. static void *agent_info_init(struct agent_info *s)
  128. {
  129. int cpu;
  130. const char *in_path = NULL;
  131. const char *out_path = NULL;
  132. /* init read/write threads */
  133. for (cpu = 0; cpu < s->cpus; cpu++) {
  134. /* set read(input) path per read/write thread */
  135. in_path = make_input_path(cpu);
  136. if (in_path == NULL)
  137. goto error;
  138. /* set write(output) path per read/write thread*/
  139. if (!s->use_stdout) {
  140. out_path = make_output_path(cpu);
  141. if (out_path == NULL)
  142. goto error;
  143. } else
  144. /* stdout mode */
  145. pr_debug("stdout mode\n");
  146. rw_thread_init(cpu, in_path, out_path, s->use_stdout,
  147. s->pipe_size, s->rw_ti[cpu]);
  148. }
  149. /* init controller of read/write threads */
  150. s->ctl_fd = rw_ctl_init((const char *)CTL_PATH);
  151. return NULL;
  152. error:
  153. exit(EXIT_FAILURE);
  154. }
  155. static void *parse_args(int argc, char *argv[], struct agent_info *s)
  156. {
  157. int cmd;
  158. unsigned long size;
  159. while ((cmd = getopt(argc, argv, "hos:")) != -1) {
  160. switch (cmd) {
  161. /* stdout mode */
  162. case 'o':
  163. s->use_stdout = true;
  164. break;
  165. /* size of pipe */
  166. case 's':
  167. size = parse_size(optarg);
  168. if (size == 0)
  169. goto error;
  170. s->pipe_size = size;
  171. break;
  172. case 'h':
  173. default:
  174. usage(argv[0]);
  175. goto error;
  176. }
  177. }
  178. agent_info_init(s);
  179. return NULL;
  180. error:
  181. exit(EXIT_FAILURE);
  182. }
  183. static void agent_main_loop(struct agent_info *s)
  184. {
  185. int cpu;
  186. pthread_t rw_thread_per_cpu[MAX_CPUS];
  187. /* Start all read/write threads */
  188. for (cpu = 0; cpu < s->cpus; cpu++)
  189. rw_thread_per_cpu[cpu] = rw_thread_run(s->rw_ti[cpu]);
  190. rw_ctl_loop(s->ctl_fd);
  191. /* Finish all read/write threads */
  192. for (cpu = 0; cpu < s->cpus; cpu++) {
  193. int ret;
  194. ret = pthread_join(rw_thread_per_cpu[cpu], NULL);
  195. if (ret != 0) {
  196. pr_err("pthread_join() error:%d (cpu %d)\n", ret, cpu);
  197. exit(EXIT_FAILURE);
  198. }
  199. }
  200. }
  201. static void agent_info_free(struct agent_info *s)
  202. {
  203. int i;
  204. close(s->ctl_fd);
  205. for (i = 0; i < s->cpus; i++) {
  206. close(s->rw_ti[i]->in_fd);
  207. close(s->rw_ti[i]->out_fd);
  208. close(s->rw_ti[i]->read_pipe);
  209. close(s->rw_ti[i]->write_pipe);
  210. free(s->rw_ti[i]);
  211. }
  212. free(s);
  213. }
  214. int main(int argc, char *argv[])
  215. {
  216. struct agent_info *s = NULL;
  217. s = agent_info_new();
  218. parse_args(argc, argv, s);
  219. agent_main_loop(s);
  220. agent_info_free(s);
  221. return 0;
  222. }