-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_processor.py
More file actions
298 lines (244 loc) · 10.8 KB
/
batch_processor.py
File metadata and controls
298 lines (244 loc) · 10.8 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""
Batch Processing Script for C Code Datasets
Processes SV-COMP and CodeNet datasets with the optimizer
"""
import os
import sys
import glob
import json
from pathlib import Path
from datetime import datetime
from typing import Dict, List, Tuple
import traceback
from parser import CCodeParser, CParserError
from dataflow_analysis import ReachingDefinitionsAnalysis, LiveVariableAnalysis
from optimizer import Optimizer
class DatasetProcessor:
"""Processes C code files from datasets"""
def __init__(self, output_dir: str = "./dataset_results"):
self.output_dir = output_dir
os.makedirs(output_dir, exist_ok=True)
self.parser = CCodeParser()
self.results = {
'total_files': 0,
'successful': 0,
'failed': 0,
'errors': [],
'statistics': []
}
def process_directory(self, directory: str, pattern: str = "*.c",
dataset_name: str = "Unknown") -> Dict:
"""Process all C files in a directory"""
print(f"\n{'='*70}")
print(f"Processing {dataset_name}")
print(f"Directory: {directory}")
print(f"{'='*70}\n")
# Find all C files
file_pattern = os.path.join(directory, "**", pattern)
files = glob.glob(file_pattern, recursive=True)
print(f"Found {len(files)} C files")
self.results['total_files'] = len(files)
# Process each file
for idx, filepath in enumerate(files, 1):
try:
print(f"[{idx}/{len(files)}] Processing {os.path.relpath(filepath, directory)}...",
end=" ", flush=True)
stats = self._process_file(filepath)
self.results['successful'] += 1
self.results['statistics'].append({
'file': filepath,
'dataset': dataset_name,
**stats
})
print(f"✓ ({stats['functions']} functions, {stats['total_insts']} insts)")
except CParserError as e:
self.results['failed'] += 1
self.results['errors'].append({
'file': filepath,
'error': str(e),
'type': 'parse_error'
})
print(f"✗ Parse error")
except Exception as e:
self.results['failed'] += 1
self.results['errors'].append({
'file': filepath,
'error': str(e),
'type': 'processing_error'
})
print(f"✗ Processing error")
return self.results
def _process_file(self, filepath: str) -> Dict:
"""Process a single C file and return statistics"""
# Parse
program = self.parser.parse_file(filepath)
stats = {
'functions': len(program.cfgs),
'total_blocks': 0,
'total_insts': 0,
'optimizations': {},
'analysis_times': {}
}
# Process each function
for func_name, cfg in program.cfgs.items():
# Count blocks and instructions
blocks = len(cfg.blocks)
insts = sum(len(b.instructions) for b in cfg.blocks.values())
stats['total_blocks'] += blocks
stats['total_insts'] += insts
# Data flow analysis
try:
rd = ReachingDefinitionsAnalysis(cfg)
rd.analyze()
lv = LiveVariableAnalysis(cfg)
lv.analyze()
uninitialized = rd.find_uninitialized_vars()
dead = lv.find_dead_assignments()
if uninitialized or dead:
stats['analysis_issues'] = {
'uninitialized_vars': len(uninitialized),
'dead_assignments': sum(len(d) for d in dead.values())
}
except:
pass
# Optimization
try:
optimizer = Optimizer(cfg)
optimizer.optimize()
opt_insts = sum(len(b.instructions) for b in cfg.blocks.values())
reduction = insts - opt_insts
if func_name not in stats['optimizations']:
stats['optimizations'][func_name] = {
'original_insts': insts,
'optimized_insts': opt_insts,
'reduction': reduction,
'reduction_pct': 100 * reduction / insts if insts > 0 else 0
}
except:
pass
return stats
def generate_report(self, output_file: str = "processing_report.json"):
"""Generate processing report"""
output_path = os.path.join(self.output_dir, output_file)
# Add timestamp
self.results['timestamp'] = datetime.now().isoformat()
self.results['success_rate'] = (
100 * self.results['successful'] / self.results['total_files']
if self.results['total_files'] > 0 else 0
)
with open(output_path, 'w') as f:
json.dump(self.results, f, indent=2, default=str)
return output_path
def print_summary(self):
"""Print processing summary"""
print(f"\n{'='*70}")
print("PROCESSING SUMMARY")
print(f"{'='*70}")
print(f"Total files: {self.results['total_files']}")
print(f"Successful: {self.results['successful']}")
print(f"Failed: {self.results['failed']}")
print(f"Success rate: {self.results['success_rate']:.1f}%")
if self.results['statistics']:
# Calculate aggregate statistics
total_funcs = sum(s['functions'] for s in self.results['statistics'])
total_blocks = sum(s['total_blocks'] for s in self.results['statistics'])
total_insts = sum(s['total_insts'] for s in self.results['statistics'])
print(f"\nAggregate Statistics:")
print(f" Total functions: {total_funcs}")
print(f" Total blocks: {total_blocks}")
print(f" Total instructions: {total_insts}")
# Optimization statistics
total_reduction = 0
opt_count = 0
for stat in self.results['statistics']:
for func_name, opt in stat['optimizations'].items():
total_reduction += opt.get('reduction', 0)
opt_count += 1
if opt_count > 0:
avg_reduction = total_reduction / opt_count
print(f"\nOptimization Results:")
print(f" Functions optimized: {opt_count}")
print(f" Avg instructions removed: {avg_reduction:.1f}")
print(f" Total instructions removed: {total_reduction}")
print(f"\nErrors: {len(self.results['errors'])}")
if self.results['errors'] and len(self.results['errors']) <= 10:
for error in self.results['errors'][:10]:
print(f" - {error['file']}: {error['error'][:60]}")
print(f"\nResults saved to: {self.output_dir}")
print(f"{'='*70}\n")
def process_svcomp_dataset(svcomp_path: str, output_dir: str = "./svcomp_results"):
"""Process SV-COMP dataset"""
processor = DatasetProcessor(output_dir)
processor.process_directory(
svcomp_path,
pattern="*.c",
dataset_name="SV-COMP"
)
processor.generate_report("svcomp_report.json")
processor.print_summary()
return processor
def process_codenet_dataset(codenet_path: str, output_dir: str = "./codenet_results"):
"""Process CodeNet dataset"""
processor = DatasetProcessor(output_dir)
processor.process_directory(
codenet_path,
pattern="*.c",
dataset_name="CodeNet"
)
processor.generate_report("codenet_report.json")
processor.print_summary()
return processor
def main():
"""Main batch processing entry point"""
import argparse
parser = argparse.ArgumentParser(
description="Batch process C code datasets",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python batch_processor.py --svcomp /path/to/sv-benchmarks
python batch_processor.py --codenet /path/to/CodeNet
python batch_processor.py --svcomp /path/sv-benchmarks --codenet /path/codenet
python batch_processor.py --demo
"""
)
parser.add_argument('--svcomp', type=str, help='Path to SV-COMP dataset')
parser.add_argument('--codenet', type=str, help='Path to CodeNet dataset')
parser.add_argument('--output', type=str, default='./batch_results',
help='Output directory for all results')
parser.add_argument('--demo', action='store_true',
help='Run demo with test.c file')
args = parser.parse_args()
print("╔════════════════════════════════════════════════════════════╗")
print("║ C CODE OPTIMIZER - BATCH DATASET PROCESSOR ║")
print("╚════════════════════════════════════════════════════════════╝")
# Demo mode
if args.demo:
print("\n[DEMO MODE] Processing test.c...")
if os.path.exists("test.c"):
processor = DatasetProcessor("./demo_results")
processor.process_directory(".", pattern="test.c", dataset_name="Demo")
processor.generate_report("demo_report.json")
processor.print_summary()
else:
print("Error: test.c not found")
return
# Process SV-COMP
if args.svcomp:
if not os.path.exists(args.svcomp):
print(f"Error: SV-COMP path not found: {args.svcomp}")
else:
svcomp_output = os.path.join(args.output, "svcomp")
process_svcomp_dataset(args.svcomp, svcomp_output)
# Process CodeNet
if args.codenet:
if not os.path.exists(args.codenet):
print(f"Error: CodeNet path not found: {args.codenet}")
else:
codenet_output = os.path.join(args.output, "codenet")
process_codenet_dataset(args.codenet, codenet_output)
if not (args.svcomp or args.codenet or args.demo):
print("\nNo dataset specified. Run with --help for options.")
print("Demo: python batch_processor.py --demo")
if __name__ == "__main__":
main()