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