Program.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Reflection;
  2. using System.Runtime.CompilerServices;
  3. using JitInspect;
  4. using Lua;
  5. using Lua.Runtime;
  6. using Lua.Standard;
  7. // dotnet run --configuration Release /p:DefineConstants="CASE_MARKER"
  8. // to activate the CASE_MARKER
  9. // JitInspect can be run in Windows and Linux (MacOS is not supported yet)
  10. var luaState = LuaState.Create();
  11. luaState.OpenStandardLibraries();
  12. var closure = luaState.Load(File.ReadAllBytes(GetAbsolutePath("test.lua")), "test.lua");
  13. for (var i = 0; i < 1000; i++)
  14. {
  15. await luaState.RunAsync(closure);
  16. luaState.Stack.Clear();
  17. }
  18. var savePath = GetAbsolutePath("history");
  19. var thisDir = GetThisDirectoryName();
  20. var newJIitPath = Path.Join(thisDir, $"jit_{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.txt");
  21. var lastJitPaths = Directory.GetFiles(thisDir).Where(x => x.Contains("jit_"));
  22. if (!Directory.Exists(savePath))
  23. {
  24. Directory.CreateDirectory(savePath);
  25. }
  26. if (lastJitPaths.Any())
  27. {
  28. Console.WriteLine("Last:" + File.ReadAllLines(lastJitPaths.First())[^1]);
  29. foreach (var jitPath in lastJitPaths)
  30. {
  31. var last = jitPath;
  32. var dest = Path.Join(savePath, Path.GetFileName(jitPath));
  33. File.Move(last, dest);
  34. }
  35. }
  36. var method = typeof(LuaVirtualMachine).GetMethod("MoveNext", BindingFlags.Static | BindingFlags.NonPublic)!;
  37. using var disassembler = JitDisassembler.Create();
  38. var nextJitText = disassembler.Disassemble(method, new() { PrintInstructionAddresses = true });
  39. File.WriteAllText(newJIitPath, nextJitText);
  40. // Console.WriteLine("New:" + nextJitText.Split("\n")[^1]);
  41. static string GetThisDirectoryName([CallerFilePath] string callerFilePath = "")
  42. {
  43. return Path.GetDirectoryName(callerFilePath)!;
  44. }
  45. static string GetAbsolutePath(string relativePath, [CallerFilePath] string callerFilePath = "")
  46. {
  47. return Path.Join(Path.GetDirectoryName(callerFilePath)!, relativePath);
  48. }