Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 738c226

Browse files
authored
GH-103082: Code cleanup in instrumentation code (#103474)
1 parent e1f1464 commit 738c226

File tree

4 files changed

+42
-39
lines changed

4 files changed

+42
-39
lines changed

Include/internal/pycore_frame.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ _PyFrame_GetLocalsArray(_PyInterpreterFrame *frame)
145145
}
146146

147147
/* Fetches the stack pointer, and sets stacktop to -1.
148-
Having stacktop <= 0 ensures that invalid
149-
values are not visible to the cycle GC.
150-
We choose -1 rather than 0 to assist debugging. */
148+
Having stacktop <= 0 ensures that invalid
149+
values are not visible to the cycle GC.
150+
We choose -1 rather than 0 to assist debugging. */
151151
static inline PyObject**
152152
_PyFrame_GetStackPointer(_PyInterpreterFrame *frame)
153153
{

Python/instrumentation.c

+34-31
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,17 @@ static const uint8_t INSTRUMENTED_OPCODES[256] = {
113113
};
114114

115115
static inline bool
116-
opcode_has_event(int opcode) {
117-
return opcode < INSTRUMENTED_LINE &&
118-
INSTRUMENTED_OPCODES[opcode] > 0;
116+
opcode_has_event(int opcode)
117+
{
118+
return (
119+
opcode < INSTRUMENTED_LINE &&
120+
INSTRUMENTED_OPCODES[opcode] > 0
121+
);
119122
}
120123

121124
static inline bool
122-
is_instrumented(int opcode) {
125+
is_instrumented(int opcode)
126+
{
123127
assert(opcode != 0);
124128
assert(opcode != RESERVED);
125129
return opcode >= MIN_INSTRUMENTED_OPCODE;
@@ -339,7 +343,8 @@ dump_monitors(const char *prefix, _Py_Monitors monitors, FILE*out)
339343
/* Like _Py_GetBaseOpcode but without asserts.
340344
* Does its best to give the right answer, but won't abort
341345
* if something is wrong */
342-
int get_base_opcode_best_attempt(PyCodeObject *code, int offset)
346+
static int
347+
get_base_opcode_best_attempt(PyCodeObject *code, int offset)
343348
{
344349
int opcode = _Py_OPCODE(_PyCode_CODE(code)[offset]);
345350
if (INSTRUMENTED_OPCODES[opcode] != opcode) {
@@ -418,13 +423,15 @@ dump_instrumentation_data(PyCodeObject *code, int star, FILE*out)
418423
assert(test); \
419424
} while (0)
420425

421-
bool valid_opcode(int opcode) {
426+
static bool
427+
valid_opcode(int opcode)
428+
{
422429
if (opcode > 0 &&
423430
opcode != RESERVED &&
424431
opcode < 255 &&
425432
_PyOpcode_OpName[opcode] &&
426-
_PyOpcode_OpName[opcode][0] != '<'
427-
) {
433+
_PyOpcode_OpName[opcode][0] != '<')
434+
{
428435
return true;
429436
}
430437
return false;
@@ -550,11 +557,11 @@ de_instrument(PyCodeObject *code, int i, int event)
550557
opcode_ptr = &code->_co_monitoring->lines[i].original_opcode;
551558
opcode = *opcode_ptr;
552559
}
553-
if (opcode == INSTRUMENTED_INSTRUCTION) {
560+
if (opcode == INSTRUMENTED_INSTRUCTION) {
554561
opcode_ptr = &code->_co_monitoring->per_instruction_opcodes[i];
555562
opcode = *opcode_ptr;
556563
}
557-
int deinstrumented = DE_INSTRUMENT[opcode];
564+
int deinstrumented = DE_INSTRUMENT[opcode];
558565
if (deinstrumented == 0) {
559566
return;
560567
}
@@ -781,8 +788,7 @@ add_line_tools(PyCodeObject * code, int offset, int tools)
781788
{
782789
assert(tools_is_subset_for_event(code, PY_MONITORING_EVENT_LINE, tools));
783790
assert(code->_co_monitoring);
784-
if (code->_co_monitoring->line_tools
785-
) {
791+
if (code->_co_monitoring->line_tools) {
786792
code->_co_monitoring->line_tools[offset] |= tools;
787793
}
788794
else {
@@ -798,8 +804,7 @@ add_per_instruction_tools(PyCodeObject * code, int offset, int tools)
798804
{
799805
assert(tools_is_subset_for_event(code, PY_MONITORING_EVENT_INSTRUCTION, tools));
800806
assert(code->_co_monitoring);
801-
if (code->_co_monitoring->per_instruction_tools
802-
) {
807+
if (code->_co_monitoring->per_instruction_tools) {
803808
code->_co_monitoring->per_instruction_tools[offset] |= tools;
804809
}
805810
else {
@@ -814,11 +819,10 @@ static void
814819
remove_per_instruction_tools(PyCodeObject * code, int offset, int tools)
815820
{
816821
assert(code->_co_monitoring);
817-
if (code->_co_monitoring->per_instruction_tools)
818-
{
822+
if (code->_co_monitoring->per_instruction_tools) {
819823
uint8_t *toolsptr = &code->_co_monitoring->per_instruction_tools[offset];
820824
*toolsptr &= ~tools;
821-
if (*toolsptr == 0 ) {
825+
if (*toolsptr == 0) {
822826
de_instrument_per_instruction(code, offset);
823827
}
824828
}
@@ -843,7 +847,7 @@ call_one_instrument(
843847
assert(tstate->tracing == 0);
844848
PyObject *instrument = interp->monitoring_callables[tool][event];
845849
if (instrument == NULL) {
846-
return 0;
850+
return 0;
847851
}
848852
int old_what = tstate->what_event;
849853
tstate->what_event = event;
@@ -865,16 +869,15 @@ static const int8_t MOST_SIGNIFICANT_BITS[16] = {
865869
3, 3, 3, 3,
866870
};
867871

868-
/* We could use _Py_bit_length here, but that is designed for larger (32/64) bit ints,
869-
and can perform relatively poorly on platforms without the necessary intrinsics. */
872+
/* We could use _Py_bit_length here, but that is designed for larger (32/64)
873+
* bit ints, and can perform relatively poorly on platforms without the
874+
* necessary intrinsics. */
870875
static inline int most_significant_bit(uint8_t bits) {
871876
assert(bits != 0);
872877
if (bits > 15) {
873878
return MOST_SIGNIFICANT_BITS[bits>>4]+4;
874879
}
875-
else {
876-
return MOST_SIGNIFICANT_BITS[bits];
877-
}
880+
return MOST_SIGNIFICANT_BITS[bits];
878881
}
879882

880883
static bool
@@ -1002,8 +1005,8 @@ _Py_call_instrumentation_2args(
10021005
int
10031006
_Py_call_instrumentation_jump(
10041007
PyThreadState *tstate, int event,
1005-
_PyInterpreterFrame *frame, _Py_CODEUNIT *instr, _Py_CODEUNIT *target
1006-
) {
1008+
_PyInterpreterFrame *frame, _Py_CODEUNIT *instr, _Py_CODEUNIT *target)
1009+
{
10071010
assert(event == PY_MONITORING_EVENT_JUMP ||
10081011
event == PY_MONITORING_EVENT_BRANCH);
10091012
assert(frame->prev_instr == instr);
@@ -1309,8 +1312,8 @@ initialize_line_tools(PyCodeObject *code, _Py_Monitors *all_events)
13091312
}
13101313
}
13111314

1312-
static
1313-
int allocate_instrumentation_data(PyCodeObject *code)
1315+
static int
1316+
allocate_instrumentation_data(PyCodeObject *code)
13141317
{
13151318

13161319
if (code->_co_monitoring == NULL) {
@@ -1404,7 +1407,7 @@ static const uint8_t super_instructions[256] = {
14041407

14051408
/* Should use instruction metadata for this */
14061409
static bool
1407-
is_super_instruction(int opcode) {
1410+
is_super_instruction(uint8_t opcode) {
14081411
return super_instructions[opcode] != 0;
14091412
}
14101413

@@ -1516,7 +1519,7 @@ _Py_Instrument(PyCodeObject *code, PyInterpreterState *interp)
15161519

15171520
#define C_RETURN_EVENTS \
15181521
((1 << PY_MONITORING_EVENT_C_RETURN) | \
1519-
(1 << PY_MONITORING_EVENT_C_RAISE))
1522+
(1 << PY_MONITORING_EVENT_C_RAISE))
15201523

15211524
#define C_CALL_EVENTS \
15221525
(C_RETURN_EVENTS | (1 << PY_MONITORING_EVENT_CALL))
@@ -1561,8 +1564,8 @@ static int
15611564
check_tool(PyInterpreterState *interp, int tool_id)
15621565
{
15631566
if (tool_id < PY_MONITORING_SYS_PROFILE_ID &&
1564-
interp->monitoring_tool_names[tool_id] == NULL
1565-
) {
1567+
interp->monitoring_tool_names[tool_id] == NULL)
1568+
{
15661569
PyErr_Format(PyExc_ValueError, "tool %d is not in use", tool_id);
15671570
return -1;
15681571
}

Python/pystate.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -686,11 +686,11 @@ init_interpreter(PyInterpreterState *interp,
686686
_PyGC_InitState(&interp->gc);
687687
PyConfig_InitPythonConfig(&interp->config);
688688
_PyType_InitCache(interp);
689-
for(int i = 0; i < PY_MONITORING_UNGROUPED_EVENTS; i++) {
689+
for (int i = 0; i < PY_MONITORING_UNGROUPED_EVENTS; i++) {
690690
interp->monitors.tools[i] = 0;
691691
}
692692
for (int t = 0; t < PY_MONITORING_TOOL_IDS; t++) {
693-
for(int e = 0; e < PY_MONITORING_EVENTS; e++) {
693+
for (int e = 0; e < PY_MONITORING_EVENTS; e++) {
694694
interp->monitoring_callables[t][e] = NULL;
695695

696696
}
@@ -834,11 +834,11 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
834834

835835
Py_CLEAR(interp->audit_hooks);
836836

837-
for(int i = 0; i < PY_MONITORING_UNGROUPED_EVENTS; i++) {
837+
for (int i = 0; i < PY_MONITORING_UNGROUPED_EVENTS; i++) {
838838
interp->monitors.tools[i] = 0;
839839
}
840840
for (int t = 0; t < PY_MONITORING_TOOL_IDS; t++) {
841-
for(int e = 0; e < PY_MONITORING_EVENTS; e++) {
841+
for (int e = 0; e < PY_MONITORING_EVENTS; e++) {
842842
Py_CLEAR(interp->monitoring_callables[t][e]);
843843
}
844844
}

Python/specialize.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ print_spec_stats(FILE *out, OpcodeStats *stats)
148148
PRIu64 "\n", i, j, val);
149149
}
150150
}
151-
for(int j = 0; j < 256; j++) {
151+
for (int j = 0; j < 256; j++) {
152152
if (stats[i].pair_count[j]) {
153153
fprintf(out, "opcode[%d].pair_count[%d] : %" PRIu64 "\n",
154154
i, j, stats[i].pair_count[j]);

0 commit comments

Comments
 (0)