-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpopulate_binaries.py
More file actions
289 lines (250 loc) · 10.4 KB
/
Copy pathpopulate_binaries.py
File metadata and controls
289 lines (250 loc) · 10.4 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
#!/usr/bin/env python3
"""
Script to populate the database with the standard binary configurations for CPython Memory Tracker.
These binaries match the frontend binary types.
"""
import asyncio
import sys
import os
# Add the parent directory to the path so we can import from app
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from app import schemas, crud, models
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
def create_database_session(database_url: str = None):
"""Create database connection from URL."""
if not database_url:
database_url = os.getenv(
"DATABASE_URL", "sqlite+aiosqlite:///./memory_tracker.db"
)
engine = create_async_engine(database_url, echo=False)
async_session = async_sessionmaker(
engine, class_=AsyncSession, expire_on_commit=False
)
return engine, async_session
async def create_tables_for_engine(engine):
"""Create tables using the provided engine."""
async with engine.begin() as conn:
await conn.run_sync(models.Base.metadata.create_all)
def get_standard_binaries():
"""Get the standard binary configurations that match the frontend."""
return [
{
"id": "default",
"name": "Default Build",
"flags": [],
"description": "Standard CPython build with default compilation settings. Used as baseline for performance comparisons.",
"color": "#3b82f6",
"icon": "settings",
"display_order": 0,
},
{
"id": "debug",
"name": "Debug Build",
"flags": ["--with-debug"],
"description": "Debug build with additional runtime checks and debugging symbols. Higher memory usage but better error detection.",
"color": "#ef4444",
"icon": "bug",
"display_order": 1,
},
{
"id": "pgo",
"name": "PGO Build",
"flags": ["--enable-optimizations"],
"description": "Profile Guided Optimization build. Uses runtime profiling data to optimize frequently executed code paths.",
"color": "#6366f1",
"icon": "zap",
"display_order": 2,
},
{
"id": "lto",
"name": "LTO Build",
"flags": ["--with-lto"],
"description": "Link Time Optimization enabled. Performs cross-module optimizations for better performance.",
"color": "#10b981",
"icon": "gauge",
"display_order": 3,
},
{
"id": "lto-pgo",
"name": "LTO + PGO Build",
"flags": ["--with-lto", "--enable-optimizations"],
"description": "Highly optimized build combining Link Time Optimization with Profile Guided Optimization. Maximum performance with cross-module optimizations and runtime profiling data.",
"color": "#8b5cf6",
"icon": "rocket",
"display_order": 4,
},
{
"id": "nogil",
"name": "No GIL Build",
"flags": ["--disable-gil"],
"description": "Experimental build without the Global Interpreter Lock (GIL). Enables true parallelism for CPU-bound tasks.",
"color": "#f59e0b",
"icon": "zap",
"display_order": 5,
},
{
"id": "debug-nogil",
"name": "Debug No GIL Build",
"flags": ["--with-debug", "--disable-gil"],
"description": "Debug build combined with no-GIL features. Best for development and testing of parallel applications.",
"color": "#a855f7",
"icon": "shield",
"display_order": 6,
},
{
"id": "trace",
"name": "Trace Build",
"flags": ["--with-trace-refs"],
"description": "Build with trace reference counting enabled. Useful for memory leak detection and debugging.",
"color": "#06b6d4",
"icon": "search",
"display_order": 7,
},
{
"id": "jit",
"name": "JIT Build",
"flags": ["--enable-experimental-jit"],
"description": "Just-In-Time compilation enabled.",
"color": "#fc03df",
"icon": "zap",
"display_order": 8,
},
]
async def populate_binaries(force: bool = False, database_url: str = None):
"""Populate the database with standard binary configurations."""
engine, AsyncSessionLocal = create_database_session(database_url)
# Ensure database tables exist
await create_tables_for_engine(engine)
async with AsyncSessionLocal() as db:
try:
binaries_data = get_standard_binaries()
created_count = 0
updated_count = 0
skipped_count = 0
print(f"Populating {len(binaries_data)} standard binary configurations...")
for binary_data in binaries_data:
binary_id = binary_data["id"]
# Check if binary already exists
existing_binary = await crud.get_binary_by_id(db, binary_id=binary_id)
if existing_binary:
if force:
# Update existing binary
existing_binary.name = binary_data["name"]
existing_binary.flags = binary_data["flags"]
existing_binary.description = binary_data["description"]
existing_binary.color = binary_data.get("color", "#8b5cf6")
existing_binary.icon = binary_data.get("icon", "server")
existing_binary.display_order = binary_data.get(
"display_order", 0
)
await db.commit()
print(f"✅ Updated binary '{binary_id}': {binary_data['name']}")
print(f" Flags: {binary_data['flags']}")
print(f" Description: {binary_data['description']}")
print(
f" Color: {binary_data.get('color', '#8b5cf6')}, Icon: {binary_data.get('icon', 'server')}"
)
updated_count += 1
else:
print(
f"⚠️ Binary '{binary_id}' already exists (use --force to update)"
)
print(
f" Current: {existing_binary.name} with flags {existing_binary.flags}"
)
skipped_count += 1
else:
# Create new binary
binary_create = schemas.BinaryCreate(
id=binary_id,
name=binary_data["name"],
flags=binary_data["flags"],
description=binary_data["description"],
color=binary_data.get("color", "#8b5cf6"),
icon=binary_data.get("icon", "server"),
display_order=binary_data.get("display_order", 0),
)
await crud.create_binary(db, binary_create)
print(f"✅ Created binary '{binary_id}': {binary_data['name']}")
print(f" Flags: {binary_data['flags']}")
print(f" Description: {binary_data['description']}")
created_count += 1
print("\n🎉 Binary population completed!")
print(f" - Created: {created_count} binaries")
print(f" - Updated: {updated_count} binaries")
print(f" - Skipped: {skipped_count} binaries")
return True
except Exception as e:
print(f"❌ Error populating binaries: {e}")
await db.rollback()
return False
async def list_binaries(database_url: str = None):
"""List all currently registered binaries."""
engine, AsyncSessionLocal = create_database_session(database_url)
await create_tables_for_engine(engine)
async with AsyncSessionLocal() as db:
try:
binaries = await crud.get_binaries(db)
if not binaries:
print("No binaries currently registered.")
return
print("Currently registered binaries:")
for binary in binaries:
flags_str = " ".join(binary.flags) if binary.flags else "none"
print(f" - {binary.id}: {binary.name} (flags: {flags_str})")
except Exception as e:
print(f"❌ Error listing binaries: {e}")
def main():
import argparse
parser = argparse.ArgumentParser(
description="Populate database with standard binary configurations",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
This script creates the standard set of binary configurations that match the frontend:
- default: Standard CPython build (baseline)
- debug: Debug build with runtime checks
- nogil: Experimental no-GIL build
- debug-nogil: Debug + no-GIL combination
- lto: Link Time Optimization enabled
- pgo: Profile Guided Optimization
- lto-pgo: Highly optimized LTO + PGO combination
- trace: Trace reference counting
- valgrind: Valgrind-optimized build
Examples:
# Populate standard binaries
python populate_binaries.py
# Force update existing binaries
python populate_binaries.py --force
# List current binaries
python populate_binaries.py --list
""",
)
parser.add_argument(
"--force",
"-f",
action="store_true",
help="Force update existing binaries with new configurations",
)
parser.add_argument(
"--list",
"-l",
action="store_true",
help="List all currently registered binaries",
)
parser.add_argument(
"--database-url",
"--db-url",
help="Database URL (default: sqlite+aiosqlite:///./memory_tracker.db or DATABASE_URL env var)",
default=None,
)
args = parser.parse_args()
if args.list:
success = asyncio.run(list_binaries(args.database_url))
else:
success = asyncio.run(
populate_binaries(force=args.force, database_url=args.database_url)
)
if not success:
sys.exit(1)
if __name__ == "__main__":
main()