Created
August 25, 2023 14:46
-
-
Save Viq111/647a3f5fe98b3ca27bf739d316547358 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dependancy: pandas | |
# Usage: | |
# curl -o Locality.cpp https://raw.githubusercontent.com/apple/foundationdb/main/fdbrpc/Locality.cpp | |
# # (You can replace /main/ by the tag version you want, for example /7.2.9/) | |
# python generate.py | |
# # output.csv is the output and can be imported in any spreadsheet editor (gdoc, excel...) for color-coding | |
from collections import defaultdict | |
import pandas as pd | |
PRIORITIES = { | |
"ProcessClass::BestFit": 0, | |
"ProcessClass::GoodFit": 1, | |
"ProcessClass::UnsetFit": 2, | |
"ProcessClass::OkayFit": 3, | |
"ProcessClass::WorstFit": 4, | |
"ProcessClass::NeverAssign": 5, | |
} | |
def string_to_role_class(s): | |
if not "ProcessClass::" in s: | |
raise IOError(s) | |
parsed = s.split("ProcessClass::")[1].split(":")[0] | |
if parsed.endswith("Class"): | |
parsed = parsed[:-5] | |
return parsed | |
def get_priority(l): | |
parsed = "ProcessClass::" + l.split("ProcessClass::")[1].split(";")[0] | |
return PRIORITIES[parsed] | |
class_to_process = defaultdict(dict) # dict[class][role] = priority | |
defaults = {} # role -> default priority | |
with open("Locality.cpp", "r") as f: | |
lines = f.readlines() | |
index = 0 | |
# Find where the switch statement starts | |
while "switch(role)" not in lines[index].replace(" ", ""): | |
index += 1 | |
index += 1 | |
# Now, iterate for each role, stops when we arrive at default: (for role) | |
while True: | |
# Get to next statement | |
while "case ProcessClass::" not in lines[index] and "default:" not in lines[index]: | |
index += 1 | |
if "default:" in lines[index]: | |
break | |
role = string_to_role_class(lines[index]) | |
index += 1 | |
classes = [] # Since a switch statement can fallthrough, collect all classes first | |
while "}" not in lines[index]: | |
# Either we have a priority (this if) OR we have a class (next) | |
if "return ProcessClass" in lines[index]: | |
priority = get_priority(lines[index]) | |
for class_ in classes: | |
c = string_to_role_class(class_) | |
class_to_process[c][role] = priority | |
classes = [] | |
elif "case ProcessClass::" in lines[index]: | |
classes.append(lines[index]) | |
elif "default:" in lines[index]: | |
defaults[role] = get_priority(lines[index+1]) | |
index += 1 | |
# Add all defaults to the final dictionary | |
for c in class_to_process.keys(): | |
for r in defaults.keys(): | |
if r not in class_to_process[c]: | |
class_to_process[c][r] = defaults[r] | |
# Generate a CSV via pandas that can be imported into any spreadsheet editor (gdoc, excel...) | |
print(class_to_process) | |
df = pd.DataFrame(class_to_process) | |
df.to_csv("output.csv") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MIT License | |
Copyright (c) 2023 Datadog, Inc. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment