Nix expr: Expose Mono's profiler

This commit is contained in:
YoshiRulz 2024-08-17 18:12:19 +10:00
parent 07508a91f2
commit 5ee53db53b
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
3 changed files with 25 additions and 6 deletions

View File

@ -12,7 +12,17 @@
, debugDotnetHostCrashes
, initConfig # pretend this is JSON; the following env. vars will be substituted by the wrapper script (if surrounded by double-percent e.g. `%%BIZHAWK_DATA_HOME%%`): `BIZHAWK_DATA_HOME`
, isManualLocalBuild # i.e. dotnet build in nix-shell; skips everything involving BIZHAWK_DATA_HOME, such as copying Assets and initConfig
, profileManagedCalls
}: let
/**
* you can make use of the call duration data by seeing which methods it's spending the longest in: `mprof-report --reports=call --method-sort=self output/*.flame.mlpd` (really you'd want to sort by self/count but that's not an option)
*
* the other useful profiling mode is allocations: `nix-shell --argstr profileManagedCalls " --profile=log:alloc,nocalls,output=%t.alloc.mlpd"`
* you can make use of the allocation data by listing which types had the most instances allocated: `mprof-report --reports=alloc --alloc-sort=count output/*.alloc.mlpd`
*/
monoProfilerFlag = if builtins.isString profileManagedCalls
then profileManagedCalls
else if profileManagedCalls then " --profile=log:noalloc,calls,zip,output=%t.flame.mlpd" else "";
hawkVersion = bizhawkAssemblies.hawkSourceInfo.version;
commentLineIf = b: lib.optionalString b "# ";
/**
@ -79,7 +89,8 @@
else
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$ldLibPath"
fi
${if bizhawkAssemblies.hawkSourceInfo.hasAssemblyResolveHandler then "" else ''export MONO_PATH="$BIZHAWK_HOME/dll/nlua:$BIZHAWK_HOME/dll"
${if profileManagedCalls == false then "" else ''printf "Will write profiling results to %s/*.mlpd\n" "$PWD"
''}${if bizhawkAssemblies.hawkSourceInfo.hasAssemblyResolveHandler then "" else ''export MONO_PATH="$BIZHAWK_HOME/dll/nlua:$BIZHAWK_HOME/dll"
''}${lib.optionalString (!debugPInvokes) "# "}export MONO_LOG_LEVEL=debug MONO_LOG_MASK=dll # pass `--arg debugPInvokes true` to nix-build to enable
${lib.optionalString debugDotnetHostCrashes "# "}export MONO_CRASH_NOFILE=1 # pass `--arg debugDotnetHostCrashes true` to nix-build to disable
if [ "$1" = '--mono-no-redirect' ]; then
@ -88,7 +99,8 @@
fi
printf "(capturing output in %s/EmuHawkMono_last*.txt)\n" "$PWD" >&2
exec '${redirectOutputToFiles}' EmuHawkMono_laststdout.txt EmuHawkMono_laststderr.txt \
'${lib.getBin bizhawkAssemblies.mono}/bin/mono' "$mainAppPath" --config=config.json "$@"
'${lib.getBin bizhawkAssemblies.mono}/bin/mono'${monoProfilerFlag} \
"$mainAppPath" --config=config.json "$@"
'';
in {
inherit emuhawk;
@ -108,9 +120,11 @@ in {
export BIZHAWK_DATA_HOME="$BIZHAWK_DATA_HOME/emuhawk-monort-${hawkVersion}"
cd "$BIZHAWK_DATA_HOME"
export MONO_PATH="$BIZHAWK_HOME/dll"
${if profileManagedCalls == false then "" else ''printf "Will write profiling results to %s/*.mlpd\n" "$PWD"
''}export MONO_PATH="$BIZHAWK_HOME/dll"
${lib.optionalString (!debugPInvokes) "# "}export MONO_LOG_LEVEL=debug MONO_LOG_MASK=dll # pass `--arg debugPInvokes true` to nix-build to enable
exec '${lib.getBin bizhawkAssemblies.mono}/bin/mono' "$BIZHAWK_HOME/DiscoHawk.exe" "$@"
exec '${lib.getBin bizhawkAssemblies.mono}/bin/mono'${monoProfilerFlag} \
"$BIZHAWK_HOME/DiscoHawk.exe" "$@"
'';
emuhawkNonNixOS = writeShellScript "emuhawk-mono-wrapper-non-nixos" ''exec '${nixGL}/bin/nixGL' '${emuhawk}' "$@"'';
}

View File

@ -68,6 +68,7 @@ in {
, extraDotnetBuildFlags ? "" # currently passed to EVERY `dotnet build` and `dotnet test` invocation (and does not replace the flags for parallel compilation added by default)
, forNixOS ? true
, initConfig ? {} # forwarded to Dist/launch-scripts.nix (see docs there)
, profileManagedCalls ? false # forwarded to Dist/launch-scripts.nix
}: let
isVersionAtLeast = lib.flip lib.versionAtLeast; # I stand by this being the more useful param order w.r.t. currying
replaceDotWithUnderscore = s: lib.replaceStrings [ "." ] [ "_" ] s;
@ -99,7 +100,7 @@ in {
inherit lib
writeShellScript writeText
bizhawkAssemblies nixGL
debugPInvokes debugDotnetHostCrashes initConfig isManualLocalBuild;
debugPInvokes debugDotnetHostCrashes initConfig isManualLocalBuild profileManagedCalls;
mkfifo = coreutils;
mktemp = coreutils;
};

View File

@ -9,11 +9,15 @@
, debugDotnetHostCrashes ? false # forwarded to Dist/launch-scripts.nix
, debugPInvokes ? false # forwarded to Dist/launch-scripts.nix
, forNixOS ? true
, profileManagedCalls ? false # forwarded to Dist/launch-scripts.nix
, useKate ? false
, useNanoAndCola ? false
, useVSCode ? false
}: let
avail = import ./. { inherit debugDotnetHostCrashes debugPInvokes forNixOS system; };
avail = import ./. {
inherit forNixOS system
debugDotnetHostCrashes debugPInvokes profileManagedCalls;
};
f = drv: mkShell {
packages = [ git powershell ]
++ lib.optionals useNanoAndCola [ git-cola nano ]