-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.py
240 lines (214 loc) · 8.64 KB
/
process.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import argparse
import os
import logging
import json
import pandas as pd
from multiprocessing import Pool
from pandas import DataFrame
from tqdm import tqdm
from collections import OrderedDict
from typing import List, Tuple, Dict, Optional
from parse.runtime_parser import RuntimeParser
from parse.raw_parser import RawParser
from parse.info_parser import InfoParser
from parse.compose_parser import ComposeParser
from parse.sum_parser import SummaryParser
from parse.context import get_trial_setup_context_from_path, TrialSetupContext
from parse.kafka_parser import PerfConsumerParser, PerfProducerParser, OpenMsgDriverParser
from genmeta.context import GenMetaContext
from genmeta.analyze import gen_stats, EmptyParsedDataError, MissingParsedLogError, UnexpectedInfoFaultNullError, EmptySlowFaultDataError
from genmeta.fields import STATS_COLNAMES, FIELD_PARSE_ERROR, FIELD_SRC_INFO, FIELD_SRC_RUNTIME, FIELD_SRC_RAW, FIELD_SRC_KAFKA
PARSERS = {
"runtime":(RuntimeParser, ["csv", "json"]),
"raw":(RawParser, ["csv"]),
"info":(InfoParser, ["json"]),
"producer":(PerfProducerParser, ["csv"]),
"consumer":(PerfConsumerParser, ["csv"]),
"driver":(OpenMsgDriverParser, ["csv"]),
"sum":(SummaryParser, ["json"]),
"compose": (ComposeParser, ["json"])
}
def get_all_files(data_dir, exts=[".log"]) -> List[str]:
paths = []
for root, dirs, files in os.walk(data_dir):
for file in files:
paths.append(os.path.join(root, file))
return [p for p in paths if os.path.splitext(p)[-1] in exts]
def parse_single(log_path, output_path_woext, parser) -> Optional[str]:
ctx = get_trial_setup_context_from_path(log_path)
data = parser.parse(log_path)
if data is None:
msg = f"Unimplemented {ctx.system} {parser.name} for {log_path}."
logging.info(msg)
return msg
else:
if isinstance(data, dict) or isinstance(data, DataFrame):
data = [data]
for d in data:
if isinstance(d, dict):
p = output_path_woext + ".json"
with open(p, 'w') as fp:
json.dump(d, fp, indent=4)
else:
try:
p = output_path_woext + ".csv"
d.to_csv(p, index=False)
except:
print("Outputing df to csv error", d)
return d
def parse_batch(data_dir, output_dir, redo_exists) -> None:
log_ctx = {}
for p in get_all_files(data_dir):
try:
log_ctx[p] = get_trial_setup_context_from_path(p)
except:
logging.info(f"Skip {p}. Cannot parse context from filename.")
worker_agrs = []
for path, ctx in tqdm(log_ctx.items()):
psrname = ctx.log_type
if psrname not in PARSERS:
continue
parser, exts = PARSERS[psrname]
outpath_woext = os.path.splitext(path.replace(data_dir, output_dir))[0]
if psrname not in redo_exists:
all_exists = True
for ext in exts:
all_exists = all_exists and os.path.exists(outpath_woext + f".{ext}")
if all_exists:
continue
os.makedirs(os.path.dirname(outpath_woext), exist_ok=True)
# parse_single(path, outpath_woext, parser()) # mute this if multiprocessing is unmuted
""" MULTIPORC STARTS """
worker_agrs.append((path, outpath_woext, parser()))
def callback(result):
if result:
print(result)
with Pool(processes=24) as pool:
for arg in worker_agrs:
pool.apply_async(parse_single, arg, callback=callback)
pool.close()
pool.join()
""" MULTIPORC ENDS """
# for progress in tqdm(pool.starmap(parse_single, worker_agrs), total=len(worker_agrs)):
# print(progress)
def hash_tsctx(ctx: TrialSetupContext) -> Tuple:
return (ctx.action, ctx.system, ctx.version, ctx.question, ctx.workload, \
ctx.cpu, ctx.mem, \
ctx.injection_location, ctx.injection_type, ctx.severity, \
ctx.start, ctx.duration, ctx.iter)
def gen_meta_batch(data_dir, output_dir) -> None:
parsed_data_files = get_all_files(data_dir, exts=[".csv", ".json"])
outpath = os.path.join(output_dir, "meta.csv")
if outpath in parsed_data_files:
parsed_data_files.remove(outpath)
genmeta_tasks: Dict[Tuple, GenMetaContext] = {}
for p in parsed_data_files:
ctx = get_trial_setup_context_from_path(p)
key = hash_tsctx(ctx)
if key not in genmeta_tasks:
genmeta_tasks[key] = GenMetaContext(ctx)
# compose
if ctx.log_type == "compose":
genmeta_tasks[key].compose_json = p
# info
if ctx.log_type == "info":
genmeta_tasks[key].info_json = p
# runtime
if ctx.system != "hadoop" and ctx.log_type == "runtime":
if p.endswith(".csv"):
genmeta_tasks[key].runtime_csv = p
elif p.endswith(".json"):
genmeta_tasks[key].runtime_json = p
# raw
if ctx.system == "hadoop" and ctx.log_type == "raw":
if ctx.workload == "mrbench":
genmeta_tasks[key].raw_mrbench_csv = p
elif ctx.workload == "terasort":
if ctx.suffix == "teragen":
genmeta_tasks[key].raw_teragen_csv = p
elif ctx.suffix == "terasort":
genmeta_tasks[key].raw_terasort_csv = p
else: raise
else: raise
# kafka
if ctx.system == "kafka":
if ctx.log_type == "producer":
genmeta_tasks[key].producer_csv = p
elif ctx.log_type == "consumer":
genmeta_tasks[key].consumer_csv = p
elif ctx.log_type == "driver":
if p.endswith(".csv"):
genmeta_tasks[key].driver_csv = p
elif p.endswith(".json"):
genmeta_tasks[key].driver_json = p
# sum
if ctx.log_type == "sum":
genmeta_tasks[key].sum_json = p
meta = []
colnames = [
"rq",
"system",
"workload",
"fault_type",
"fault_location",
"fault_duration",
"fault_start",
"fault_severity",
"iter_flag",
"cpu",
"mem"] + STATS_COLNAMES
for key, gmctx in tqdm(genmeta_tasks.items()):
full_stats = OrderedDict({k:"" for k in STATS_COLNAMES})
err = ""
stats = {}
try:
gen_stats(gmctx, stats)
except (EmptyParsedDataError,
MissingParsedLogError,
UnexpectedInfoFaultNullError,
EmptySlowFaultDataError) as e:
err = f"{type(e).__name__}:{e}"
for k in stats.keys():
assert k in STATS_COLNAMES, k
full_stats.update(stats)
full_stats[FIELD_PARSE_ERROR] = err
full_stats[FIELD_SRC_INFO] = gmctx.info_json
full_stats[FIELD_SRC_RUNTIME] = gmctx.runtime_csv
full_stats[FIELD_SRC_RAW] = f"{gmctx.raw_mrbench_csv},{gmctx.raw_terasort_csv}"
full_stats[FIELD_SRC_KAFKA] = f"{gmctx.producer_csv},{gmctx.producer_csv},{gmctx.driver_csv}"
row = [
gmctx.ctx.question,
gmctx.ctx.system + gmctx.ctx.version,
gmctx.ctx.workload,
gmctx.ctx.injection_type,
gmctx.ctx.injection_location,
gmctx.ctx.duration,
gmctx.ctx.start,
gmctx.ctx.severity,
gmctx.ctx.iter,
gmctx.ctx.cpu,
gmctx.ctx.mem] + list(full_stats.values())
meta.append(row)
df = pd.DataFrame(sorted(meta), columns=colnames)
df.to_csv(outpath, index=False)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="xinda log parser")
parser.add_argument("-d", "--data_dir", required=True,
help="Specify root of data directory containing all logs")
parser.add_argument("-o", "--output_dir", required=True,
help="Specify root of output directory for outputing all logs")
parser.add_argument("-r", "--redo", default="",
help="Redo tasks")
args = parser.parse_args()
# output_dir = os.path.abspath(args.output_dir or args.data_dir.replace("data", "processed_results"))
output_dir = os.path.abspath(args.output_dir)
parse_batch(
data_dir=os.path.abspath(args.data_dir),
output_dir=output_dir,
redo_exists=args.redo.split(",")
)
print("finish parsing, start summarizing")
gen_meta_batch(
data_dir=output_dir,
output_dir=output_dir,
)