Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * activity measurement functions. |
| 3 | * |
| 4 | * Copyright 2000-2018 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 13 | #include <haproxy/activity-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 14 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 15 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 16 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 17 | #include <haproxy/cli.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 18 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 19 | #include <haproxy/stream_interface.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 20 | #include <haproxy/tools.h> |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 21 | |
| 22 | |
| 23 | /* bit field of profiling options. Beware, may be modified at runtime! */ |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 24 | unsigned int profiling = HA_PROF_TASKS_AUTO; |
Willy Tarreau | d9add3a | 2019-04-25 08:57:41 +0200 | [diff] [blame] | 25 | unsigned long task_profiling_mask = 0; |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 26 | |
| 27 | /* One struct per thread containing all collected measurements */ |
| 28 | struct activity activity[MAX_THREADS] __attribute__((aligned(64))) = { }; |
| 29 | |
| 30 | |
| 31 | /* Updates the current thread's statistics about stolen CPU time. The unit for |
| 32 | * <stolen> is half-milliseconds. |
| 33 | */ |
| 34 | void report_stolen_time(uint64_t stolen) |
| 35 | { |
| 36 | activity[tid].cpust_total += stolen; |
| 37 | update_freq_ctr(&activity[tid].cpust_1s, stolen); |
| 38 | update_freq_ctr_period(&activity[tid].cpust_15s, 15000, stolen); |
| 39 | } |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 40 | |
| 41 | /* config parser for global "profiling.tasks", accepts "on" or "off" */ |
| 42 | static int cfg_parse_prof_tasks(char **args, int section_type, struct proxy *curpx, |
| 43 | struct proxy *defpx, const char *file, int line, |
| 44 | char **err) |
| 45 | { |
| 46 | if (too_many_args(1, args, err, NULL)) |
| 47 | return -1; |
| 48 | |
| 49 | if (strcmp(args[1], "on") == 0) |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 50 | profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_ON; |
| 51 | else if (strcmp(args[1], "auto") == 0) |
| 52 | profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AUTO; |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 53 | else if (strcmp(args[1], "off") == 0) |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 54 | profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_OFF; |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 55 | else { |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 56 | memprintf(err, "'%s' expects either 'on', 'auto', or 'off' but got '%s'.", args[0], args[1]); |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 57 | return -1; |
| 58 | } |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | /* parse a "set profiling" command. It always returns 1. */ |
| 63 | static int cli_parse_set_profiling(char **args, char *payload, struct appctx *appctx, void *private) |
| 64 | { |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 65 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 66 | return 1; |
| 67 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 68 | if (strcmp(args[2], "tasks") != 0) |
| 69 | return cli_err(appctx, "Expects 'tasks'.\n"); |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 70 | |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 71 | if (strcmp(args[3], "on") == 0) { |
| 72 | unsigned int old = profiling; |
| 73 | while (!_HA_ATOMIC_CAS(&profiling, &old, (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_ON)) |
| 74 | ; |
| 75 | } |
| 76 | else if (strcmp(args[3], "auto") == 0) { |
| 77 | unsigned int old = profiling; |
| 78 | while (!_HA_ATOMIC_CAS(&profiling, &old, (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AUTO)) |
| 79 | ; |
| 80 | } |
| 81 | else if (strcmp(args[3], "off") == 0) { |
| 82 | unsigned int old = profiling; |
| 83 | while (!_HA_ATOMIC_CAS(&profiling, &old, (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_OFF)) |
| 84 | ; |
| 85 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 86 | else |
| 87 | return cli_err(appctx, "Expects 'on', 'auto', or 'off'.\n"); |
| 88 | |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | /* This function dumps all profiling settings. It returns 0 if the output |
| 93 | * buffer is full and it needs to be called again, otherwise non-zero. |
| 94 | */ |
| 95 | static int cli_io_handler_show_profiling(struct appctx *appctx) |
| 96 | { |
| 97 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 98 | const char *str; |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 99 | |
| 100 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 101 | return 1; |
| 102 | |
| 103 | chunk_reset(&trash); |
| 104 | |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 105 | switch (profiling & HA_PROF_TASKS_MASK) { |
| 106 | case HA_PROF_TASKS_AUTO: str="auto"; break; |
| 107 | case HA_PROF_TASKS_ON: str="on"; break; |
| 108 | default: str="off"; break; |
| 109 | } |
| 110 | |
| 111 | chunk_printf(&trash, |
| 112 | "Per-task CPU profiling : %s # set profiling tasks {on|auto|off}\n", |
| 113 | str); |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 114 | |
| 115 | if (ci_putchk(si_ic(si), &trash) == -1) { |
| 116 | /* failed, try again */ |
| 117 | si_rx_room_blk(si); |
| 118 | return 0; |
| 119 | } |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | /* config keyword parsers */ |
| 124 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 125 | { CFG_GLOBAL, "profiling.tasks", cfg_parse_prof_tasks }, |
| 126 | { 0, NULL, NULL } |
| 127 | }}; |
| 128 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 129 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 130 | |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 131 | /* register cli keywords */ |
| 132 | static struct cli_kw_list cli_kws = {{ },{ |
| 133 | { { "show", "profiling", NULL }, "show profiling : show CPU profiling options", NULL, cli_io_handler_show_profiling, NULL }, |
| 134 | { { "set", "profiling", NULL }, "set profiling : enable/disable CPU profiling", cli_parse_set_profiling, NULL }, |
| 135 | {{},} |
| 136 | }}; |
| 137 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 138 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |