blob: 0d64a36591316a66fa57ea733a7bd63349ae4ab9 [file] [log] [blame]
Willy Tarreau609aad92018-11-22 08:31:09 +01001/*
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 Tarreaub2551052020-06-09 09:07:15 +020013#include <haproxy/activity-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020015#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020016#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020017#include <haproxy/cli.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020018#include <haproxy/freq_ctr.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020019#include <haproxy/stream_interface.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020020#include <haproxy/tools.h>
Willy Tarreau75c62c22018-11-22 11:02:09 +010021
22
23/* bit field of profiling options. Beware, may be modified at runtime! */
Willy Tarreauaa622b82021-01-28 21:44:22 +010024unsigned int profiling = HA_PROF_TASKS_AOFF;
Willy Tarreaud9add3a2019-04-25 08:57:41 +020025unsigned long task_profiling_mask = 0;
Willy Tarreau609aad92018-11-22 08:31:09 +010026
27/* One struct per thread containing all collected measurements */
28struct activity activity[MAX_THREADS] __attribute__((aligned(64))) = { };
29
Willy Tarreau3fb6a7b2021-01-28 19:19:26 +010030/* One struct per function pointer hash entry (256 values, 0=collision) */
31struct sched_activity sched_activity[256] __attribute__((aligned(64))) = { };
Willy Tarreau609aad92018-11-22 08:31:09 +010032
33/* Updates the current thread's statistics about stolen CPU time. The unit for
34 * <stolen> is half-milliseconds.
35 */
36void 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 Tarreau75c62c22018-11-22 11:02:09 +010042
43/* config parser for global "profiling.tasks", accepts "on" or "off" */
44static 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 Tarreaud2d33482019-04-25 17:09:07 +020052 profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_ON;
53 else if (strcmp(args[1], "auto") == 0)
Willy Tarreauaa622b82021-01-28 21:44:22 +010054 profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AOFF;
Willy Tarreau75c62c22018-11-22 11:02:09 +010055 else if (strcmp(args[1], "off") == 0)
Willy Tarreaud2d33482019-04-25 17:09:07 +020056 profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_OFF;
Willy Tarreau75c62c22018-11-22 11:02:09 +010057 else {
Willy Tarreaud2d33482019-04-25 17:09:07 +020058 memprintf(err, "'%s' expects either 'on', 'auto', or 'off' but got '%s'.", args[0], args[1]);
Willy Tarreau75c62c22018-11-22 11:02:09 +010059 return -1;
60 }
61 return 0;
62}
63
64/* parse a "set profiling" command. It always returns 1. */
65static int cli_parse_set_profiling(char **args, char *payload, struct appctx *appctx, void *private)
66{
Willy Tarreau75c62c22018-11-22 11:02:09 +010067 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
68 return 1;
69
Willy Tarreau9d008692019-08-09 11:21:01 +020070 if (strcmp(args[2], "tasks") != 0)
71 return cli_err(appctx, "Expects 'tasks'.\n");
Willy Tarreau75c62c22018-11-22 11:02:09 +010072
Willy Tarreaud2d33482019-04-25 17:09:07 +020073 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;
Willy Tarreauaa622b82021-01-28 21:44:22 +010080 unsigned int new;
81
82 do {
83 if ((old & HA_PROF_TASKS_MASK) >= HA_PROF_TASKS_AON)
84 new = (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AON;
85 else
86 new = (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AOFF;
87 } while (!_HA_ATOMIC_CAS(&profiling, &old, new));
Willy Tarreaud2d33482019-04-25 17:09:07 +020088 }
89 else if (strcmp(args[3], "off") == 0) {
90 unsigned int old = profiling;
91 while (!_HA_ATOMIC_CAS(&profiling, &old, (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_OFF))
92 ;
93 }
Willy Tarreau9d008692019-08-09 11:21:01 +020094 else
95 return cli_err(appctx, "Expects 'on', 'auto', or 'off'.\n");
96
Willy Tarreau75c62c22018-11-22 11:02:09 +010097 return 1;
98}
99
100/* This function dumps all profiling settings. It returns 0 if the output
101 * buffer is full and it needs to be called again, otherwise non-zero.
102 */
103static int cli_io_handler_show_profiling(struct appctx *appctx)
104{
105 struct stream_interface *si = appctx->owner;
Willy Tarreaud2d33482019-04-25 17:09:07 +0200106 const char *str;
Willy Tarreau75c62c22018-11-22 11:02:09 +0100107
108 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
109 return 1;
110
111 chunk_reset(&trash);
112
Willy Tarreaud2d33482019-04-25 17:09:07 +0200113 switch (profiling & HA_PROF_TASKS_MASK) {
Willy Tarreauaa622b82021-01-28 21:44:22 +0100114 case HA_PROF_TASKS_AOFF: str="auto-off"; break;
115 case HA_PROF_TASKS_AON: str="auto-on"; break;
Willy Tarreaud2d33482019-04-25 17:09:07 +0200116 case HA_PROF_TASKS_ON: str="on"; break;
117 default: str="off"; break;
118 }
119
120 chunk_printf(&trash,
121 "Per-task CPU profiling : %s # set profiling tasks {on|auto|off}\n",
122 str);
Willy Tarreau75c62c22018-11-22 11:02:09 +0100123
124 if (ci_putchk(si_ic(si), &trash) == -1) {
125 /* failed, try again */
126 si_rx_room_blk(si);
127 return 0;
128 }
129 return 1;
130}
131
132/* config keyword parsers */
133static struct cfg_kw_list cfg_kws = {ILH, {
134 { CFG_GLOBAL, "profiling.tasks", cfg_parse_prof_tasks },
135 { 0, NULL, NULL }
136}};
137
Willy Tarreau0108d902018-11-25 19:14:37 +0100138INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
139
Willy Tarreau75c62c22018-11-22 11:02:09 +0100140/* register cli keywords */
141static struct cli_kw_list cli_kws = {{ },{
142 { { "show", "profiling", NULL }, "show profiling : show CPU profiling options", NULL, cli_io_handler_show_profiling, NULL },
143 { { "set", "profiling", NULL }, "set profiling : enable/disable CPU profiling", cli_parse_set_profiling, NULL },
144 {{},}
145}};
146
Willy Tarreau0108d902018-11-25 19:14:37 +0100147INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);