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