|
7 | 7 | import requests
|
8 | 8 |
|
9 | 9 | from sinol_make import util
|
10 |
| - |
| 10 | +from sinol_make.executors.sio2jail import Sio2jailExecutor |
| 11 | +from sinol_make.structs.status_structs import Status |
11 | 12 |
|
12 | 13 | def sio2jail_supported():
|
13 | 14 | return util.is_linux()
|
@@ -74,29 +75,74 @@ def install_sio2jail(directory=None):
|
74 | 75 |
|
75 | 76 | def check_perf_counters_enabled():
|
76 | 77 | """
|
77 |
| - Checks if `kernel.perf_event_paranoid` is set to -1. |
78 |
| - :return: |
| 78 | + Checks if sio2jail is able to use perf counters to count instructions. |
79 | 79 | """
|
80 |
| - if not util.is_linux() or not check_sio2jail(): |
| 80 | + if not sio2jail_supported() or not check_sio2jail(): |
81 | 81 | return
|
82 | 82 |
|
83 |
| - sio2jail = get_default_sio2jail_path() |
| 83 | + with open('/proc/sys/kernel/perf_event_paranoid') as f: |
| 84 | + perf_event_paranoid = int(f.read()) |
| 85 | + |
| 86 | + executor = Sio2jailExecutor(get_default_sio2jail_path()) |
84 | 87 | test_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'perf_test.py')
|
85 | 88 | python_executable = sys.executable
|
86 |
| - |
87 |
| - # subprocess.Pipe is not used, because than the code would hang on process.communicate() |
88 |
| - with tempfile.TemporaryFile() as tmpfile: |
89 |
| - process = subprocess.Popen([sio2jail, '--mount-namespace', 'off', '--', python_executable, test_file], |
90 |
| - stdout=tmpfile, stderr=subprocess.DEVNULL) |
91 |
| - process.wait() |
92 |
| - tmpfile.seek(0) |
93 |
| - output = tmpfile.read().decode('utf-8') |
94 |
| - process.terminate() |
95 |
| - |
96 |
| - if output != "Test string\n": |
97 |
| - util.exit_with_error("To use the recommended tool for measuring time called `sio2jail`, please:\n" |
98 |
| - "- execute `sudo sysctl kernel.perf_event_paranoid=-1` to make `sio2jail` work for\n" |
99 |
| - " the current system session,\n" |
100 |
| - "- or add `kernel.perf_event_paranoid=-1` to `/etc/sysctl.conf`\n" |
101 |
| - " and reboot to permanently make sio2jail work.\n" |
102 |
| - "For more details, see https://github.com/sio2project/sio2jail#running.\n") |
| 89 | + command = [python_executable, test_file] |
| 90 | + time_limit = 1000 |
| 91 | + memory_limit = 65536 |
| 92 | + |
| 93 | + with ( |
| 94 | + tempfile.NamedTemporaryFile() as sio2jail_result, |
| 95 | + tempfile.TemporaryFile() as command_stdout, |
| 96 | + tempfile.TemporaryFile() as command_stderr |
| 97 | + ): |
| 98 | + result = executor.execute( |
| 99 | + command=command, |
| 100 | + time_limit=time_limit, |
| 101 | + hard_time_limit=None, |
| 102 | + memory_limit=memory_limit, |
| 103 | + result_file_path=sio2jail_result.name, |
| 104 | + executable=None, |
| 105 | + execution_dir=None, |
| 106 | + stdout=command_stdout, |
| 107 | + stderr=command_stderr) |
| 108 | + command_stdout.seek(0) |
| 109 | + output_str = command_stdout.read().decode('utf-8') |
| 110 | + command_stderr.seek(0) |
| 111 | + error_str = command_stderr.read().decode('utf-8') |
| 112 | + sio2jail_result.seek(0) |
| 113 | + result_raw = sio2jail_result.read().decode('utf-8') |
| 114 | + |
| 115 | + expected_output = "Test Successful!\n" |
| 116 | + if result.Status != Status.OK or output_str != expected_output or error_str: |
| 117 | + max_perf_event_paranoid = 2 |
| 118 | + if perf_event_paranoid > max_perf_event_paranoid: |
| 119 | + hint = (f"You have sysctl kernel.perf_event_paranoid = {perf_event_paranoid}" |
| 120 | + "\nThis might restrict access to instruction counting." |
| 121 | + "\nTry relaxing this setting by running:" |
| 122 | + f"\n\tsudo sysctl kernel.perf_event_paranoid={max_perf_event_paranoid}" |
| 123 | + "\nIf that fixes the problem, you can set this permanently by adding:" |
| 124 | + f"\n\tkernel.perf_event_paranoid={max_perf_event_paranoid}" |
| 125 | + "\nto /etc/sysctl.conf and rebooting." |
| 126 | + ) |
| 127 | + else: |
| 128 | + hint = ("Your kernel, drivers, or hardware might be unsupported." |
| 129 | + "\nDiagnose this further by trying the following commands:" |
| 130 | + "\n1. Check if the `perf` tool is able to read performance counters correctly:" |
| 131 | + "\n\tperf stat -e instructions:u -- sleep 0" |
| 132 | + "\nIf `perf` can't be found, it might be located in: /usr/lib/linux-tools/*/perf" |
| 133 | + "\n2. Check if the Performance Monitoring Unit driver was successfully loaded:" |
| 134 | + "\n\tdmesg | grep PMU" |
| 135 | + ) |
| 136 | + opt_stdout_hint = f"\nCommand stdout (expected {repr(expected_output)}):\n---\n{output_str}" if output_str != expected_output else "" |
| 137 | + opt_stderr_hint = f"\nCommand stderr (expected none):\n---\n{error_str}" if error_str else "" |
| 138 | + opt_sio2jail_hint = f"\nsio2jail result:\n---\n{result_raw}" if result.Status != Status.OK else "" |
| 139 | + util.exit_with_error("Failed sio2jail instruction counting self-check!" |
| 140 | + f"\n\nTest command:\n---\n{result.Cmdline}\n" |
| 141 | + f"{opt_stdout_hint}" |
| 142 | + f"{opt_stderr_hint}" |
| 143 | + f"{opt_sio2jail_hint}" |
| 144 | + f"\n\n{hint}" |
| 145 | + "\n\nYou can also disable instruction counting by adding the `--time-tool time` flag." |
| 146 | + "\nThis will make measured solution run times significantly different from SIO2." |
| 147 | + "\nFor more details, see https://github.com/sio2project/sio2jail#running." |
| 148 | + ) |
0 commit comments