diff --git a/crates/coa-core/src/config/mod.rs b/crates/coa-core/src/config/mod.rs index a3eb738..2ee7744 100644 --- a/crates/coa-core/src/config/mod.rs +++ b/crates/coa-core/src/config/mod.rs @@ -355,6 +355,67 @@ pub fn list_snapshots(meta_dir: &Path) -> Vec { out } +/// How many threads the world should use to update maps on this computer: half the logical processors, between 2 and 8. +/// Maps (continents, dungeons, battlegrounds) are updated in parallel, one thread per map, so more threads than the +/// number of maps in use bring nothing. +pub fn recommended_map_threads() -> u32 { + let cores = std::thread::available_parallelism().map(|n| n.get() as u32).unwrap_or(4); + (cores / 2).clamp(2, 8) +} + +const PERFORMANCE_MARKER: &str = "performance-defaults-v1.done"; + +/// Once per server: switch the world to updating maps on several threads (`MapUpdate.Threads`), which the stock +/// configuration leaves at 1. A value the owner already raised is respected, and because this runs a single time +/// (a marker file in the Manager's folder for the server) a later choice to go back to 1 is never overridden. +/// Returns the new thread count when something was written. +pub fn ensure_performance_defaults(root: &Path, meta_dir: &Path) -> Result> { + let marker = meta_dir.join(PERFORMANCE_MARKER); + if marker.exists() { + return Ok(None); + } + let view = load(root, Scope::Server)?; + let current = view.settings.iter().find(|s| s.meta.key == "MapUpdate.Threads"); + let already = current.map(|s| s.present && s.value.as_i64().unwrap_or(1) > 1).unwrap_or(false); + let threads = recommended_map_threads(); + let written = if already { + None + } else { + let changes = BTreeMap::from([("MapUpdate.Threads".to_string(), Value::from(threads))]); + save(root, meta_dir, Scope::Server, &changes)?; + Some(threads) + }; + let quieted = quiet_debug_loggers(root)?; + fsx::atomic_write(&marker, b"MapUpdate.Threads and logging levels set by the Manager\n")?; + tracing::info!(?written, ?quieted, "performance defaults applied"); + Ok(written) +} + +/// Loggers some configurations leave at debug level: `network` writes a line for every packet sent and +/// `entities.player` one for every item check. With companions that is thousands of lines a second, each a disk write. +/// They are put back to info level (the next level up); anything else in the file is untouched. +fn quiet_debug_loggers(root: &Path) -> Result> { + let mut changed = Vec::new(); + for path in targets(root, Scope::Server)?.writes { + let mut conf = ConfFile::parse_bytes(&fs::read(&path)?)?; + let mut touched = false; + for key in ["Logger.network", "Logger.entities.player"] { + let Some(value) = conf.get(key).map(|v| v.trim().to_string()) else { continue }; + if let Some(rest) = value.strip_prefix("5,") { + conf.set(key, &format!("4,{rest}"), &["Lowered from debug by CoA Server Manager: it wrote a line for every packet and item check"]); + touched = true; + if !changed.contains(&key.to_string()) { + changed.push(key.to_string()); + } + } + } + if touched { + fsx::atomic_write(&path, conf.to_text().as_bytes())?; + } + } + Ok(changed) +} + /// Apply validated `changes`. Nothing is written unless every change is valid; previous contents are snapshotted first. pub fn save(root: &Path, meta_dir: &Path, scope: Scope, changes: &BTreeMap) -> Result { let raws = validate(root, scope, changes)?; @@ -508,6 +569,38 @@ mod tests { pairs.iter().map(|(k, v)| (k.to_string(), v.clone())).collect() } + #[test] + fn the_world_gets_several_map_threads_once_and_an_owner_choice_is_never_overridden() { + let (_d, root, meta) = fixture(); + let wanted = recommended_map_threads(); + assert!((2..=8).contains(&wanted)); + for p in ["Settings/worldserver.conf.template", "Core/configs/worldserver.conf"] { + let mut t = fs::read_to_string(root.join(p)).unwrap(); + t.push_str("Logger.network=5,Server\r\nLogger.entities.player=5,Server\r\nLogger.module=4,Console Server\r\n"); + fs::write(root.join(p), t).unwrap(); + } + assert_eq!(ensure_performance_defaults(&root, &meta).unwrap(), Some(wanted)); + for p in ["Settings/worldserver.conf.template", "Core/configs/worldserver.conf"] { + let t = fs::read_to_string(root.join(p)).unwrap(); + assert!(t.contains(&format!("MapUpdate.Threads = {wanted}")), "{p}: {t}"); + assert!(t.contains("Unknown.Custom = \"keep me\""), "other settings untouched"); + assert!(t.contains("Logger.network=4,Server") && t.contains("Logger.entities.player=4,Server"), "{p}: {t}"); + assert!(t.contains("Logger.module=4,Console Server"), "info level loggers are left alone"); + } + // the owner goes back to one thread: it stays that way + save(&root, &meta, Scope::Server, &set(&[("MapUpdate.Threads", json!(1))])).unwrap(); + assert_eq!(ensure_performance_defaults(&root, &meta).unwrap(), None); + assert!(fs::read_to_string(root.join("Core/configs/worldserver.conf")).unwrap().contains("MapUpdate.Threads = 1")); + } + + #[test] + fn an_already_raised_value_is_respected_on_the_first_run() { + let (_d, root, meta) = fixture(); + save(&root, &meta, Scope::Server, &set(&[("MapUpdate.Threads", json!(6))])).unwrap(); + assert_eq!(ensure_performance_defaults(&root, &meta).unwrap(), None); + assert!(fs::read_to_string(root.join("Core/configs/worldserver.conf")).unwrap().contains("MapUpdate.Threads = 6")); + } + #[test] fn missing_module_configs_are_created_from_their_dist_and_existing_ones_are_kept() { let (_d, root, _m) = fixture(); diff --git a/schemas/server.json b/schemas/server.json index cb3072b..14d5e3b 100644 --- a/schemas/server.json +++ b/schemas/server.json @@ -489,6 +489,19 @@ "max": 1000, "unit": "ms" }, + { + "key": "MapUpdate.Threads", + "type": "int", + "category": "performance", + "title": "CPU cores for the world", + "description": "How many processor cores the world uses to update maps. Companions spread over several maps run much faster with more than one. Takes effect when the world server restarts.", + "default": 4, + "advanced": false, + "restartRequired": "world", + "dangerous": false, + "min": 1, + "max": 16 + }, { "key": "MinWorldUpdateTime", "type": "int", diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index fe83db0..69af757 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -199,6 +199,9 @@ async fn start_server(state: State<'_, AppState>, id: String) -> std::result::Re } let dir = meta_dir(&root)?; coa_core::friends::ensure_bind(&root, &dir)?; + if meta.kind == coa_core::registry::InstallKind::New { + coa_core::config::ensure_performance_defaults(&root, &dir)?; + } Ok::<(), Error>(()) }) .await; diff --git a/src/i18n/schema/de.json b/src/i18n/schema/de.json index 4c9c669..dcfe99e 100644 --- a/src/i18n/schema/de.json +++ b/src/i18n/schema/de.json @@ -462,6 +462,10 @@ "Updates.EnableDatabases": { "t": "Automatische Datenbank-Updates", "d": "Erlaubt dem Server, seine Datenbanken beim Start selbst zu ändern. Lass es auf 0: Der Manager wendet Datenbank-Updates sicher an und verfolgt sie." + }, + "MapUpdate.Threads": { + "t": "CPU-Kerne für die Welt", + "d": "Wie viele Prozessorkerne die Welt zum Aktualisieren der Karten nutzt. Begleiter, die sich über mehrere Karten verteilen, laufen mit mehr als einem Kern deutlich schneller. Wird nach einem Neustart des Weltservers wirksam." } }, "options": { diff --git a/src/i18n/schema/es.json b/src/i18n/schema/es.json index ffa69be..9df1d7a 100644 --- a/src/i18n/schema/es.json +++ b/src/i18n/schema/es.json @@ -462,6 +462,10 @@ "Updates.EnableDatabases": { "t": "Actualizaciones automáticas de bases de datos", "d": "Permite que el servidor cambie él mismo sus bases de datos al arrancar. Déjalo en 0: el Manager aplica y registra las actualizaciones de bases de datos de forma segura." + }, + "MapUpdate.Threads": { + "t": "Núcleos de CPU para el mundo", + "d": "Cuántos núcleos del procesador usa el mundo para actualizar los mapas. Los compañeros repartidos por varios mapas van mucho más rápido con más de un núcleo. Se aplica al reiniciar el servidor del mundo." } }, "options": { diff --git a/src/i18n/schema/fr.json b/src/i18n/schema/fr.json index 125bb11..8d7ead2 100644 --- a/src/i18n/schema/fr.json +++ b/src/i18n/schema/fr.json @@ -462,6 +462,10 @@ "Updates.EnableDatabases": { "t": "Mises à jour automatiques des bases de données", "d": "Permet au serveur de modifier lui-même ses bases de données au démarrage. Gardez 0 : le Manager applique et suit les mises à jour de base de données en toute sécurité." + }, + "MapUpdate.Threads": { + "t": "Cœurs du processeur pour le monde", + "d": "Nombre de cœurs du processeur utilisés par le monde pour mettre à jour les cartes. Les compagnons répartis sur plusieurs cartes vont nettement plus vite avec plus d’un cœur. Pris en compte au redémarrage du serveur de monde." } }, "options": { diff --git a/src/i18n/schema/ru.json b/src/i18n/schema/ru.json index e602a4b..748b1bf 100644 --- a/src/i18n/schema/ru.json +++ b/src/i18n/schema/ru.json @@ -462,6 +462,10 @@ "Updates.EnableDatabases": { "t": "Автоматические обновления баз данных", "d": "Позволяет серверу самому менять свои базы данных при запуске. Оставьте 0: менеджер безопасно применяет и учитывает обновления баз данных." + }, + "MapUpdate.Threads": { + "t": "Ядра процессора для мира", + "d": "Сколько ядер процессора мир использует для обновления карт. Компаньоны, разбросанные по нескольким картам, работают заметно быстрее при значении больше одного. Применяется после перезапуска игрового сервера." } }, "options": { diff --git a/tools/gen_server_schema.py b/tools/gen_server_schema.py index f22a31b..e2f10f4 100644 --- a/tools/gen_server_schema.py +++ b/tools/gen_server_schema.py @@ -39,6 +39,7 @@ R = [ ("AutoBroadcast.Timer", "int", "world", "Announcement interval", "Time between automatic announcements.", 60000, 5000, 3600000, "ms", True, "world", False), ("Warden.Enabled", "bool", "advanced", "Anti-cheat (Warden)", "Enables the client anti-cheat check. Usually unnecessary on a private server.", True, None, None, None, True, "world", False), ("MapUpdateInterval", "int", "performance", "Map update interval", "How often the world updates maps. Lower is smoother but uses more CPU.", 10, 1, 1000, "ms", True, "world", False), + ("MapUpdate.Threads", "int", "performance", "CPU cores for the world", "How many processor cores the world uses to update maps. Companions spread over several maps run much faster with more than one. Takes effect when the world server restarts.", 4, 1, 16, None, False, "world", False), ("MinWorldUpdateTime", "int", "performance", "Minimum world update time", "Shortest time between world updates. Raise it to reduce CPU use.", 1, 1, 1000, "ms", True, "world", False), ("ThreadPool", "int", "performance", "Worker threads", "Number of background worker threads. More helps large populations.", 2, 1, 32, None, True, "full", False), ("Network.Threads", "int", "network", "Network threads", "Threads that handle player connections. One is enough for small servers.", 1, 1, 16, None, True, "full", False), diff --git a/tools/sample_stacks.py b/tools/sample_stacks.py new file mode 100644 index 0000000..63ec246 --- /dev/null +++ b/tools/sample_stacks.py @@ -0,0 +1,247 @@ +"""Poor man's sampling profiler for a running worldserver (Windows, 64-bit, needs worldserver.pdb next to the exe). + + python tools/sample_stacks.py [seconds=30] [hz=100] [--threads N] [--depth D] [--out report.txt] + +Every 1/hz seconds each busy thread is suspended for a moment, its call stack is walked with dbghelp and symbolised from +the PDB, then the thread is resumed. Prints, per thread, the functions that were on the stack most often (inclusive) and +the ones that were running (self). Only for diagnosis: sampling slows the process a little. +""" +import argparse +import collections +import ctypes +import ctypes.wintypes as wt +import os +import sys +import time + +k32 = ctypes.WinDLL("kernel32", use_last_error=True) +dbg = ctypes.WinDLL("dbghelp") + +THREAD_ALL = 0x1FFFFF +PROCESS_ALL = 0x1FFFFF +CONTEXT_AMD64 = 0x100000 +CONTEXT_CONTROL = CONTEXT_AMD64 | 0x1 +CONTEXT_INTEGER = CONTEXT_AMD64 | 0x2 +TH32CS_SNAPTHREAD = 0x4 + + +class THREADENTRY32(ctypes.Structure): + _fields_ = [("dwSize", wt.DWORD), ("cntUsage", wt.DWORD), ("th32ThreadID", wt.DWORD), ("th32OwnerProcessID", wt.DWORD), + ("tpBasePri", wt.LONG), ("tpDeltaPri", wt.LONG), ("dwFlags", wt.DWORD)] + + +class M128A(ctypes.Structure): + _fields_ = [("Low", ctypes.c_ulonglong), ("High", ctypes.c_longlong)] + + +class CONTEXT(ctypes.Structure): + _pack_ = 16 + _fields_ = [("P1Home", ctypes.c_ulonglong), ("P2Home", ctypes.c_ulonglong), ("P3Home", ctypes.c_ulonglong), + ("P4Home", ctypes.c_ulonglong), ("P5Home", ctypes.c_ulonglong), ("P6Home", ctypes.c_ulonglong), + ("ContextFlags", wt.DWORD), ("MxCsr", wt.DWORD), + ("SegCs", wt.WORD), ("SegDs", wt.WORD), ("SegEs", wt.WORD), ("SegFs", wt.WORD), ("SegGs", wt.WORD), ("SegSs", wt.WORD), + ("EFlags", wt.DWORD), + ("Dr0", ctypes.c_ulonglong), ("Dr1", ctypes.c_ulonglong), ("Dr2", ctypes.c_ulonglong), ("Dr3", ctypes.c_ulonglong), + ("Dr6", ctypes.c_ulonglong), ("Dr7", ctypes.c_ulonglong), + ("Rax", ctypes.c_ulonglong), ("Rcx", ctypes.c_ulonglong), ("Rdx", ctypes.c_ulonglong), ("Rbx", ctypes.c_ulonglong), + ("Rsp", ctypes.c_ulonglong), ("Rbp", ctypes.c_ulonglong), ("Rsi", ctypes.c_ulonglong), ("Rdi", ctypes.c_ulonglong), + ("R8", ctypes.c_ulonglong), ("R9", ctypes.c_ulonglong), ("R10", ctypes.c_ulonglong), ("R11", ctypes.c_ulonglong), + ("R12", ctypes.c_ulonglong), ("R13", ctypes.c_ulonglong), ("R14", ctypes.c_ulonglong), ("R15", ctypes.c_ulonglong), + ("Rip", ctypes.c_ulonglong), + ("FltSave", ctypes.c_ubyte * 512), + ("VectorRegister", M128A * 26), ("VectorControl", ctypes.c_ulonglong), + ("DebugControl", ctypes.c_ulonglong), ("LastBranchToRip", ctypes.c_ulonglong), ("LastBranchFromRip", ctypes.c_ulonglong), + ("LastExceptionToRip", ctypes.c_ulonglong), ("LastExceptionFromRip", ctypes.c_ulonglong)] + + +class ADDRESS64(ctypes.Structure): + _fields_ = [("Offset", ctypes.c_ulonglong), ("Segment", wt.WORD), ("Mode", wt.DWORD)] + + +class KDHELP64(ctypes.Structure): + _fields_ = [("Thread", ctypes.c_ulonglong), ("ThCallbackStack", wt.DWORD), ("ThCallbackBStore", wt.DWORD), + ("NextCallback", wt.DWORD), ("FramePointer", wt.DWORD), ("KiCallUserMode", ctypes.c_ulonglong), + ("KeUserCallbackDispatcher", ctypes.c_ulonglong), ("SystemRangeStart", ctypes.c_ulonglong), + ("KiUserExceptionDispatcher", ctypes.c_ulonglong), ("StackBase", ctypes.c_ulonglong), + ("StackLimit", ctypes.c_ulonglong), ("Reserved", ctypes.c_ulonglong * 5)] + + +class STACKFRAME64(ctypes.Structure): + _fields_ = [("AddrPC", ADDRESS64), ("AddrReturn", ADDRESS64), ("AddrFrame", ADDRESS64), ("AddrStack", ADDRESS64), + ("AddrBStore", ADDRESS64), ("FuncTableEntry", ctypes.c_void_p), ("Params", ctypes.c_ulonglong * 4), + ("Far", wt.BOOL), ("Virtual", wt.BOOL), ("Reserved", ctypes.c_ulonglong * 3), ("KdHelp", KDHELP64)] + + +MAX_NAME = 512 + + +class SYMBOL_INFO(ctypes.Structure): + _fields_ = [("SizeOfStruct", wt.ULONG), ("TypeIndex", wt.ULONG), ("Reserved", ctypes.c_ulonglong * 2), ("Index", wt.ULONG), + ("Size", wt.ULONG), ("ModBase", ctypes.c_ulonglong), ("Flags", wt.ULONG), ("Value", ctypes.c_ulonglong), + ("Address", ctypes.c_ulonglong), ("Register", wt.ULONG), ("Scope", wt.ULONG), ("Tag", wt.ULONG), + ("NameLen", wt.ULONG), ("MaxNameLen", wt.ULONG), ("Name", ctypes.c_char * MAX_NAME)] + + +k32.OpenProcess.restype = wt.HANDLE +k32.OpenThread.restype = wt.HANDLE +k32.GetCurrentProcess.restype = wt.HANDLE +dbg.SymFunctionTableAccess64.restype = ctypes.c_void_p +dbg.SymGetModuleBase64.restype = ctypes.c_ulonglong +dbg.SymFunctionTableAccess64.argtypes = [wt.HANDLE, ctypes.c_ulonglong] +dbg.SymGetModuleBase64.argtypes = [wt.HANDLE, ctypes.c_ulonglong] +dbg.StackWalk64.argtypes = [wt.DWORD, wt.HANDLE, wt.HANDLE, ctypes.POINTER(STACKFRAME64), ctypes.c_void_p, ctypes.c_void_p, + ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p] +dbg.SymFromAddr.argtypes = [wt.HANDLE, ctypes.c_ulonglong, ctypes.POINTER(ctypes.c_ulonglong), ctypes.POINTER(SYMBOL_INFO)] +dbg.SymInitialize.argtypes = [wt.HANDLE, ctypes.c_char_p, wt.BOOL] +dbg.SymSetOptions.argtypes = [wt.DWORD] + +_name_cache = {} + + +def symbol(proc, addr): + # cache by 64-byte block: good enough to group samples, saves a lookup per frame + key = addr >> 4 + hit = _name_cache.get(key) + if hit is not None: + return hit + info = SYMBOL_INFO() + info.SizeOfStruct = 88 # sizeof(SYMBOL_INFO) in the Windows headers (Name[1]) + info.MaxNameLen = MAX_NAME - 1 + disp = ctypes.c_ulonglong(0) + if dbg.SymFromAddr(proc, addr, ctypes.byref(disp), ctypes.byref(info)): + name = info.Name[: info.NameLen].decode("utf-8", "replace") + else: + name = f"?{addr:#x}" + _name_cache[key] = name + return name + + +def threads_of(pid): + snap = k32.CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0) + out = [] + te = THREADENTRY32() + te.dwSize = ctypes.sizeof(te) + ok = k32.Thread32First(snap, ctypes.byref(te)) + while ok: + if te.th32OwnerProcessID == pid: + out.append(te.th32ThreadID) + ok = k32.Thread32Next(snap, ctypes.byref(te)) + k32.CloseHandle(snap) + return out + + +def cpu_time(h): + c, e, kt, ut = (wt.FILETIME() for _ in range(4)) + k32.GetThreadTimes(h, ctypes.byref(c), ctypes.byref(e), ctypes.byref(kt), ctypes.byref(ut)) + f = lambda x: (x.dwHighDateTime << 32 | x.dwLowDateTime) / 1e7 + return f(kt) + f(ut) + + +def aligned_context(): + raw = ctypes.create_string_buffer(ctypes.sizeof(CONTEXT) + 32) + addr = ctypes.addressof(raw) + off = (-addr) % 16 + ctx = CONTEXT.from_buffer(raw, off) + return raw, ctx + + +def stack(proc, th, depth): + raw, ctx = aligned_context() + ctx.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER + if k32.SuspendThread(th) == 0xFFFFFFFF: + return None + try: + if not k32.GetThreadContext(th, ctypes.byref(ctx)): + return None + frame = STACKFRAME64() + frame.AddrPC.Offset, frame.AddrPC.Mode = ctx.Rip, 3 + frame.AddrFrame.Offset, frame.AddrFrame.Mode = ctx.Rsp, 3 + frame.AddrStack.Offset, frame.AddrStack.Mode = ctx.Rsp, 3 + addrs = [ctx.Rip] + for _ in range(depth): + ok = dbg.StackWalk64(0x8664, proc, th, ctypes.byref(frame), ctypes.byref(ctx), None, + dbg.SymFunctionTableAccess64, dbg.SymGetModuleBase64, None) + if not ok or frame.AddrPC.Offset == 0: + break + addrs.append(frame.AddrPC.Offset) + finally: + k32.ResumeThread(th) + return addrs + + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("pid", type=int) + ap.add_argument("seconds", type=float, nargs="?", default=30) + ap.add_argument("hz", type=float, nargs="?", default=100) + ap.add_argument("--threads", type=int, default=5) + ap.add_argument("--depth", type=int, default=40) + ap.add_argument("--out") + ap.add_argument("--symbols", help="symbol path (folder with worldserver.pdb)") + ap.add_argument("--under", action="append", default=[], help="function name: also report what it spends its time in (its direct callees); repeatable") + a = ap.parse_args() + + proc = k32.OpenProcess(PROCESS_ALL, False, a.pid) + if not proc: + sys.exit("cannot open the process (run as the same user)") + dbg.SymSetOptions(0x2 | 0x4 | 0x10 | 0x200) # UNDNAME | DEFERRED_LOADS | LOAD_LINES | FAIL_CRITICAL_ERRORS off + if not dbg.SymInitialize(proc, a.symbols.encode() if a.symbols else None, True): + sys.exit("SymInitialize failed") + + tids = threads_of(a.pid) + handles = {t: k32.OpenThread(THREAD_ALL, False, t) for t in tids} + t0 = {t: cpu_time(h) for t, h in handles.items()} + time.sleep(2) + busy = sorted(((cpu_time(h) - t0[t]) / 2 * 100, t) for t, h in handles.items())[::-1] + chosen = [t for pct, t in busy[: a.threads] if pct > 3] + print("sampling threads:", ", ".join(f"{t} ({pct:.0f}%)" for pct, t in busy[: a.threads] if pct > 3), flush=True) + + incl = {t: collections.Counter() for t in chosen} + selfc = {t: collections.Counter() for t in chosen} + samples = {t: 0 for t in chosen} + callees = {t: {u: collections.Counter() for u in a.under} for t in chosen} + period = 1.0 / a.hz + end = time.time() + a.seconds + first = True + while time.time() < end: + for t in chosen: + addrs = stack(proc, handles[t], a.depth) + if not addrs: + continue + names = [symbol(proc, ad) for ad in addrs] + samples[t] += 1 + selfc[t][names[0]] += 1 + for n in set(names): + incl[t][n] += 1 + for u in a.under: + for i, n in enumerate(names): + if n == u and i > 0: + callees[t][u][names[i - 1]] += 1 + break + if first: + print("first samples taken", flush=True) + first = False + time.sleep(period) + + lines = [] + for t in chosen: + n = max(1, samples[t]) + lines.append(f"\n=== thread {t}: {samples[t]} samples") + lines.append("-- running (self) --") + for name, c in selfc[t].most_common(25): + lines.append(f"{c * 100 / n:5.1f}% {name[:150]}") + lines.append("-- on the stack (inclusive) --") + for name, c in incl[t].most_common(45): + lines.append(f"{c * 100 / n:5.1f}% {name[:150]}") + for u in a.under: + lines.append(f"-- inside {u}: where its time goes (share of ALL samples) --") + for name, c in callees[t][u].most_common(15): + lines.append(f"{c * 100 / n:5.1f}% {name[:150]}") + text = "\n".join(lines) + print(text) + if a.out: + open(a.out, "w", encoding="utf-8").write(text) + + +if __name__ == "__main__": + main()