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 , 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` , 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 , isManualLocalBuild # i.e. dotnet build in nix-shell; skips everything involving BIZHAWK_DATA_HOME, such as copying Assets and initConfig
, profileManagedCalls
}: let }: 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; hawkVersion = bizhawkAssemblies.hawkSourceInfo.version;
commentLineIf = b: lib.optionalString b "# "; commentLineIf = b: lib.optionalString b "# ";
/** /**
@ -79,7 +89,8 @@
else else
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$ldLibPath" export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$ldLibPath"
fi 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 (!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 ${lib.optionalString debugDotnetHostCrashes "# "}export MONO_CRASH_NOFILE=1 # pass `--arg debugDotnetHostCrashes true` to nix-build to disable
if [ "$1" = '--mono-no-redirect' ]; then if [ "$1" = '--mono-no-redirect' ]; then
@ -88,7 +99,8 @@
fi fi
printf "(capturing output in %s/EmuHawkMono_last*.txt)\n" "$PWD" >&2 printf "(capturing output in %s/EmuHawkMono_last*.txt)\n" "$PWD" >&2
exec '${redirectOutputToFiles}' EmuHawkMono_laststdout.txt EmuHawkMono_laststderr.txt \ 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 { in {
inherit emuhawk; inherit emuhawk;
@ -108,9 +120,11 @@ in {
export BIZHAWK_DATA_HOME="$BIZHAWK_DATA_HOME/emuhawk-monort-${hawkVersion}" export BIZHAWK_DATA_HOME="$BIZHAWK_DATA_HOME/emuhawk-monort-${hawkVersion}"
cd "$BIZHAWK_DATA_HOME" 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 ${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}' "$@"''; 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) , 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 , forNixOS ? true
, initConfig ? {} # forwarded to Dist/launch-scripts.nix (see docs there) , initConfig ? {} # forwarded to Dist/launch-scripts.nix (see docs there)
, profileManagedCalls ? false # forwarded to Dist/launch-scripts.nix
}: let }: let
isVersionAtLeast = lib.flip lib.versionAtLeast; # I stand by this being the more useful param order w.r.t. currying isVersionAtLeast = lib.flip lib.versionAtLeast; # I stand by this being the more useful param order w.r.t. currying
replaceDotWithUnderscore = s: lib.replaceStrings [ "." ] [ "_" ] s; replaceDotWithUnderscore = s: lib.replaceStrings [ "." ] [ "_" ] s;
@ -99,7 +100,7 @@ in {
inherit lib inherit lib
writeShellScript writeText writeShellScript writeText
bizhawkAssemblies nixGL bizhawkAssemblies nixGL
debugPInvokes debugDotnetHostCrashes initConfig isManualLocalBuild; debugPInvokes debugDotnetHostCrashes initConfig isManualLocalBuild profileManagedCalls;
mkfifo = coreutils; mkfifo = coreutils;
mktemp = coreutils; mktemp = coreutils;
}; };

View File

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