Skip to content

Commit

Permalink
minor bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoliy Bilenko authored and Anatoliy Bilenko committed Jul 19, 2024
1 parent 83b2b6e commit 808971e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
4 changes: 3 additions & 1 deletion chronoscope/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def parse_args():
help="tick identifier to plot")
parser.add_argument("-S", "--spans", type=str, default="[]",
help="histogram spans")
parser.add_argument("-v", "--verbose", type=str, default=False,
help="print more information")
return parser.parse_args()


Expand All @@ -66,7 +68,7 @@ def main() -> int:
match args.command:
case "create":
db.open(args.db, db_options, create=True)
db.load(parser.parser(args.conf), args.trace)
db.load(parser.parser(args.conf, args.verbose), args.trace)
db.mkidx()
db.close()
case "chart":
Expand Down
2 changes: 1 addition & 1 deletion chronoscope/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def line_nr(file: str) -> int:
result = sp.run(['wc', file], stdout=sp.PIPE, text=True)
return int(result.stdout.split()[0])

def load(pr: pr.parser, trace_path: str, fd_chunk_size=100, db_chunk_size=10):
def load(pr: pr.parser, trace_path: str, fd_chunk_size=900, db_chunk_size=100):
if not os.path.exists(trace_path):
raise FileNotFoundError("`{trace_path}' not found!")

Expand Down
18 changes: 12 additions & 6 deletions chronoscope/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import chronoscope.utils as u
from typing import Callable
import yaml
import sys

class parser:
def __init__(self, conf_path: str):
def __init__(self, conf_path: str, verbose=False):
# type -> (dest_table, parser_func)
self.parsers: dict[str, tuple[str, Callable]] = {}
# the parser knows about table names
self.tables = ["tick", "attr", "relation"]
self.verbose = verbose
self.load_config(conf_path)

def load_config(self, conf_path: str):
Expand Down Expand Up @@ -48,7 +50,7 @@ def parse(line: list[str], parse_type: str):
"time": u.ns(line[time]),
"type": line[type], "event": line[event],
"id": u.pack(int(line[id]), int(line[pid]))
} if parse_type == line[type] else None
} if type < len(line) and parse_type == line[type] else None
return parse

def make_rel_parser(self, orig_id: int, dest_id: int,
Expand All @@ -58,7 +60,7 @@ def parse(line: list[str], parse_type: str):
"orig": u.pack(int(line[orig_id]), int(line[orig_pid])),
"dest": u.pack(int(line[dest_id]), int(line[orig_pid])),
"type": line[type]
} if parse_type == line[type] else None
} if type < len(line) and parse_type == line[type] else None
return parse

def make_attr_parser(self, id: int, pid: int,
Expand All @@ -68,7 +70,7 @@ def parse(line: list[str], parse_type: str):
"id": u.pack(int(line[id]), int(line[pid])),
"val": line[value],
"name": line[name],
} if parse_type == line[type] else None
} if type < len(line) and parse_type == line[type] else None
return parse

def register_parser(self, dest_table: str, type: str, parse: Callable):
Expand All @@ -79,6 +81,10 @@ def parse(self,
records: dict[str, list] = {t: [] for t in self.tables}
for line in fd_chunk:
for p_name, (dest_table, parser) in self.parsers.items():
if record := parser(line.split(), p_name):
records[dest_table].append(record)
try:
if record := parser(line.split(), p_name):
records[dest_table].append(record)
except Exception as e:
if self.verbose:
print(f"{e}: {line=}", file=sys.stderr)
return records
16 changes: 16 additions & 0 deletions chronoscope/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,19 @@ def test_parser():
libuv[3433]: 2055-11-29T20:57:56.489282133 conn pid: 111 sm_id: 1 started |
"""
_ = parser("test/chronoscope.yaml").parse([line])

def test_parser_malformed():
line = """
libuv[3433]: 2055-11-29T20:57:56.667095559 conn
"""
_ = parser("test/chronoscope.yaml", verbose=True).parse([line])

def test_parser_fuzz():
line = """
a b c d
"""
_ = parser("test/chronoscope.yaml").parse([line])

def test_parser_empty():
line = ""
_ = parser("test/chronoscope.yaml").parse([line])
5 changes: 2 additions & 3 deletions chronoscope/vcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def plot(origin: int, depth_max=50):
db.iterate(origin, None, db.tick, v, 0, depth_max)

timetable.sort(key=lambda t: t["time"])
time0 = timetable[0]["time"]
t0 = timetable[0]["time"]
for t in timetable:
t["time"] -= time0
writer.change(counters[t["var"]], t["time"], t["event"])
writer.change(counters[t["var"]], t["time"] - t0, t["event"])

0 comments on commit 808971e

Please sign in to comment.