Skip to content

Commit 30984cc

Browse files
committed
rv: Refactor da_monitor to minimise macros
The da_monitor helper functions are generated from macros of the type: DECLARE_DA_FUNCTION(name, type) \ static void da_func_x_##name(type arg) {} \ static void da_func_y_##name(type arg) {} \ This is good to minimise code duplication but the long macros made of skipped end of lines is rather hard to parse. Since functions are static, the advantage of naming them differently for each monitor is minimal. Refactor the da_monitor.h file to minimise macros, instead of declaring functions from macros, we simply declare them with the same name for all monitors (e.g. da_func_x) and for any remaining reference to the monitor name (e.g. tracepoints, enums, global variables) we use the CONCATENATE macro. In this way the file is much easier to maintain while keeping the same generality. Functions depending on the monitor types are now conditionally compiled according to the value of RV_MON_TYPE, which must be defined in the monitor source. The monitor type can be specified as in the original implementation, although it's best to keep the default implementation (unsigned char) as not all parts of code support larger data types, and likely there's no need. We keep the empty macro definitions to ease review of this change with diff tools, but cleanup is required. Also adapt existing monitors to keep the build working. Reviewed-by: Nam Cao <namcao@linutronix.de> Link: https://lore.kernel.org/r/20251126104241.291258-2-gmonaco@redhat.com Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>
1 parent 0f61b18 commit 30984cc

23 files changed

Lines changed: 682 additions & 676 deletions

File tree

include/linux/rv.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#define MAX_DA_NAME_LEN 32
1111
#define MAX_DA_RETRY_RACING_EVENTS 3
1212

13+
#define RV_MON_GLOBAL 0
14+
#define RV_MON_PER_CPU 1
15+
#define RV_MON_PER_TASK 2
16+
1317
#ifdef CONFIG_RV
1418
#include <linux/array_size.h>
1519
#include <linux/bitops.h>

include/rv/automata.h

Lines changed: 73 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,84 @@
66
* models in C generated by the dot2k tool.
77
*/
88

9+
#ifndef MONITOR_NAME
10+
#error "MONITOR_NAME macro is not defined. Did you include $(MODEL_NAME).h generated by rvgen?"
11+
#endif
12+
13+
#ifndef type
14+
#define type unsigned char
15+
#endif
16+
17+
#define RV_AUTOMATON_NAME CONCATENATE(automaton_, MONITOR_NAME)
18+
#define EVENT_MAX CONCATENATE(event_max_, MONITOR_NAME)
19+
#define STATE_MAX CONCATENATE(state_max_, MONITOR_NAME)
20+
#define events CONCATENATE(events_, MONITOR_NAME)
21+
#define states CONCATENATE(states_, MONITOR_NAME)
22+
923
/*
1024
* DECLARE_AUTOMATA_HELPERS - define a set of helper functions for automata
1125
*
1226
* Define a set of helper functions for automata. The 'name' argument is used
1327
* as suffix for the functions and data. These functions will handle automaton
1428
* with data type 'type'.
1529
*/
16-
#define DECLARE_AUTOMATA_HELPERS(name, type) \
17-
\
18-
/* \
19-
* model_get_state_name_##name - return the (string) name of the given state \
20-
*/ \
21-
static char *model_get_state_name_##name(enum states_##name state) \
22-
{ \
23-
if ((state < 0) || (state >= state_max_##name)) \
24-
return "INVALID"; \
25-
\
26-
return automaton_##name.state_names[state]; \
27-
} \
28-
\
29-
/* \
30-
* model_get_event_name_##name - return the (string) name of the given event \
31-
*/ \
32-
static char *model_get_event_name_##name(enum events_##name event) \
33-
{ \
34-
if ((event < 0) || (event >= event_max_##name)) \
35-
return "INVALID"; \
36-
\
37-
return automaton_##name.event_names[event]; \
38-
} \
39-
\
40-
/* \
41-
* model_get_initial_state_##name - return the automaton's initial state \
42-
*/ \
43-
static inline type model_get_initial_state_##name(void) \
44-
{ \
45-
return automaton_##name.initial_state; \
46-
} \
47-
\
48-
/* \
49-
* model_get_next_state_##name - process an automaton event occurrence \
50-
* \
51-
* Given the current state (curr_state) and the event (event), returns \
52-
* the next state, or INVALID_STATE in case of error. \
53-
*/ \
54-
static inline type model_get_next_state_##name(enum states_##name curr_state, \
55-
enum events_##name event) \
56-
{ \
57-
if ((curr_state < 0) || (curr_state >= state_max_##name)) \
58-
return INVALID_STATE; \
59-
\
60-
if ((event < 0) || (event >= event_max_##name)) \
61-
return INVALID_STATE; \
62-
\
63-
return automaton_##name.function[curr_state][event]; \
64-
} \
65-
\
66-
/* \
67-
* model_is_final_state_##name - check if the given state is a final state \
68-
*/ \
69-
static inline bool model_is_final_state_##name(enum states_##name state) \
70-
{ \
71-
if ((state < 0) || (state >= state_max_##name)) \
72-
return 0; \
73-
\
74-
return automaton_##name.final_states[state]; \
30+
#define DECLARE_AUTOMATA_HELPERS(name, type)
31+
32+
/*
33+
* model_get_state_name - return the (string) name of the given state
34+
*/
35+
static char *model_get_state_name(enum states state)
36+
{
37+
if ((state < 0) || (state >= STATE_MAX))
38+
return "INVALID";
39+
40+
return RV_AUTOMATON_NAME.state_names[state];
41+
}
42+
43+
/*
44+
* model_get_event_name - return the (string) name of the given event
45+
*/
46+
static char *model_get_event_name(enum events event)
47+
{
48+
if ((event < 0) || (event >= EVENT_MAX))
49+
return "INVALID";
50+
51+
return RV_AUTOMATON_NAME.event_names[event];
52+
}
53+
54+
/*
55+
* model_get_initial_state - return the automaton's initial state
56+
*/
57+
static inline type model_get_initial_state(void)
58+
{
59+
return RV_AUTOMATON_NAME.initial_state;
60+
}
61+
62+
/*
63+
* model_get_next_state - process an automaton event occurrence
64+
*
65+
* Given the current state (curr_state) and the event (event), returns
66+
* the next state, or INVALID_STATE in case of error.
67+
*/
68+
static inline type model_get_next_state(enum states curr_state,
69+
enum events event)
70+
{
71+
if ((curr_state < 0) || (curr_state >= STATE_MAX))
72+
return INVALID_STATE;
73+
74+
if ((event < 0) || (event >= EVENT_MAX))
75+
return INVALID_STATE;
76+
77+
return RV_AUTOMATON_NAME.function[curr_state][event];
78+
}
79+
80+
/*
81+
* model_is_final_state - check if the given state is a final state
82+
*/
83+
static inline bool model_is_final_state(enum states state)
84+
{
85+
if ((state < 0) || (state >= STATE_MAX))
86+
return 0;
87+
88+
return RV_AUTOMATON_NAME.final_states[state];
7589
}

0 commit comments

Comments
 (0)