trace-agent.c 5.1 KB

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