从没有堆栈跟踪的“B”或“C”调用的方法“A”MethodBase/MethodInfo获取调用堆栈

如何解决从没有堆栈跟踪的“B”或“C”调用的方法“A”MethodBase/MethodInfo获取调用堆栈

好吧,我想在 Minecraft (https://github.com/lucko/spark) 中做我自己的基准测试系统,比如 spark:

...

我正在使用 Harmony lib (https://github.com/pardeike/Harmony),它允许我交互/修改方法,并允许我在每次调用时添加前缀/后缀,这将帮助我解决这个堆栈。

基本结构类似于(https://github.com/pardeike/Harmony/issues/355):

[HarmonyPatch] 
    class MyPatches
    {
        static IEnumerable<MethodBase> TargetMethods()
        {
            return AccessTools.GetTypesFromAssembly(Assembly.GetExecutingAssembly())
                .SelectMany(type => type.GetMethods())
                .Where(method => method.ReturnType != typeof(void) && method.Name.StartsWith("Do"));
        }

        static void Prefix(out Stopwatch __state,MethodBase __originalMethod)
        {
            __state = Stopwatch.StartNew();
            // ...
        }
        static void Postfix(Stopwatch __state,MethodBase __originalMethod)
        {
            __state.Stop();
            // ....
        }
    }

这里的问题是 __originalMethod 不关心它是从 A 还是 B 调用的。

例如,我们修补了 string.Join 方法。我们从 A 或 B 调用,其中 A 或 B 是此方法的完整调用堆栈。

所以首先,我们需要为这个调用分配一个ID,我们需要创建一个基于树的结构(以后很难序列化),从这里(https://stackoverflow.com/a/36649069/3286975):

public class TreeModel : Tree<TreeModel>
{
    public int ID { get; set; }
    public TreeModel() { }
    public TreeModel(TreeModel parent) : base(parent) { }
}

public class Tree<T> where T : Tree<T>
{
    protected Tree() : this(null) { }
    protected Tree(T parent)
    {
        Parent=parent;
        Children=new List<T>();
        if(parent!=null)
        {
            parent.Children.Add(this as T);
        }
    }
    public T Parent { get; set; }
    public List<T> Children { get; set; }
    public bool IsRoot { get { return Parent==null; } }
    public T Root { get { return IsRoot?this as T:Parent.Root; } }
    public T RecursiveFind(Predicate<T> check)
    {
        if(check(this as T)) return this as T;
        foreach(var item in Children)
        {
            var result=item.RecursiveFind(check);
            if(result!=null)
            {
                return result;
            }
        }
        return null;
    }
}

现在,只要我们迭代从 Harmony 获得的所有方法和指令,我们就需要填充树。暂时忘掉 Harmony,我将只解释两个事实。

lib 允许您首先通过 IEnumerable<MethodBase> TargetMethods() 获取所有修补的方法,因此,您让 Assembly X 通过反射并过滤了所有允许修补的方法(其中一些破坏了 Unity,所以我决定跳过来自 UnityEngine.、UnityEditor. 和 System.* 命名空间的方法)。

我们还有来自给定 MethodBase 的 ReadMethodBody 方法 (https://harmony.pardeike.net/api/HarmonyLib.PatchProcessor.html#HarmonyLib_PatchProcessor_ReadMethodBody_System_Reflection_MethodBase_),它返回所有 IL 堆栈指令。

所以我们可以开始一遍又一遍地迭代以获取所有指令并填充整个树。这是我昨晚写的:

    internal static class BenchmarkEnumerator
    {
        internal static Dictionary<MethodBase,int> Mappings { get; } = new Dictionary<MethodBase,int>();
        internal static Dictionary<int,TreeModel> TreeIDs { get; } = new Dictionary<int,TreeModel>();
        internal static Dictionary<MethodBase,BenchmarkTreeModel> TreeMappings { get; } = new Dictionary<MethodBase,BenchmarkTreeModel>();

        private static HashSet<int> IDUsed { get; } = new HashSet<int>();

        public static int GetID(this MethodBase method)
        {
            return GetID(method,out _);
        }

        public static int GetID(this MethodBase method,out bool contains)
        {
            // A > X = X1
            // B > X = X2

            if (!Mappings.ContainsKey(method))
            {
                var id = Mappings.Count;
                Mappings.Add(method,Mappings.Count);
                IDUsed.Add(id);
                contains = false;
                return id;
            }

            contains = true;
            return Mappings[method];
        }

        public static int GetFreeID()
        {
            int id;
            Random rnd = new Random();
            do
            {
                id = rnd.Next();
            } while (IDUsed.Contains(id));

            IDUsed.Add(id);

            return id;
        }

        public static BenchmarkCall GetCall(int id)
        {
            return TreeIDs[id]?.Call;
        }

        public static BenchmarkCall GetCall(this MethodBase method)
        {
            return TreeIDs[Mappings[method]]?.Call;
        }
    }

BenchmarkEnumerator 类允许我们区分 A 或 B,但它不关心完整的层次结构,只关心父 MethodBase 本身,所以我需要写一些复杂的东西来处理完整的调用堆栈,我说我有一个问题要理解。

然后我们有 TargetMethods:

 private static IEnumerable<MethodBase> TargetMethods()
        {
            Model = new BenchmarkTreeModel();

            var sw = Stopwatch.StartNew();
            //int i = 0;
            return Filter.GetTargetMethods(method =>
            {
                try
                {
                    var instructions = PatchProcessor.ReadMethodBody(method);
                    var i = method.GetID(out var contains);
                    var tree = new TreeModel
                    {
                        ID = i
                    };
                    if (contains)
                    {
                        //var lastId = i;
                        i = GetFreeID();
                        tree.ID = i;
                        tree.FillMethodName($"{method.GetMethodSignature()}_{i}"); // TODO: Check this
                        tree.Parent = null;
                        tree.Children = TreeMappings[method].Forest.First().Children; // ??

                        //DictionaryHelper.AddOrAppend(TreeMappings,method,tree);
                        TreeMappings[method].Forest.Add(tree);
                        TreeIDs.Add(i,tree);

                        Model.Forest.Add(tree);

                        // UNIT TESTING: All contained methods at this point will have a parent.
                        // string.Join is being added as a method by a instruction,so when we try to patch it,it will have already a reference on the dictionary
                        // Here,we check if the method was already added by a instruction CALL
                        // Logic: If the method is already contained by the mapping dictionary
                        // then,we will exit adding a new that will have the same childs but a new ID
                        return false;
                    }

                    TreeIDs.Add(i,tree);
                    tree.FillMethodName($"{method.GetMethodSignature()}_{i}"); // TODO: Check this
                    foreach (var pair in instructions)
                    {
                        var opcode = pair.Key;
                        if (opcode != OpCodes.Call || opcode != OpCodes.Callvirt) continue;
                        var childMethod = (MethodBase)pair.Value;
                        var id = childMethod.GetID(out var _contains);

                        var subTree = new TreeModel(tree)
                        {
                            ID = id
                        };
                        if (_contains)
                        {
                            id = GetFreeID();
                            subTree.ID = id;
                            subTree.FillMethodName($"{childMethod.GetMethodSignature()}_{id}"); // TODO: Check this
                            subTree.Parent = TreeIDs[i];
                            subTree.Children = TreeMappings[childMethod].Forest.First().Children;
                            TreeIDs.Add(id,subTree);
                            continue;
                        }

                        TreeIDs.Add(id,subTree);
                        subTree.FillMethodName($"{childMethod.GetMethodSignature()}_{id}");
                        tree.Children.Add(subTree);

                        TreeMappings.Add(childMethod,new BenchmarkTreeModel());
                        TreeMappings[childMethod].Forest.Add(subTree);
                    }

                    TreeMappings.Add(method,new BenchmarkTreeModel());
                    TreeMappings[method].Forest.Add(tree);

                    Model.Forest.Add(tree);

                    return true;

                    //var treeModel = new TreeModel();
                }
                catch (Exception ex)
                {
                    //Debug.LogException(new Exception(method.GetMethodSignature(),ex));
                    return false;
                }
            },sw);

            //return methods;
        }

GetMethodSignature 类似于:

        public static string GetMethodSignature(this MethodBase method)
        {
            if (method == null) return null;
            return method.DeclaringType == null ? method.Name : $"{method.DeclaringType.FullName}.{method.Name}";
        }

我想我会用 MethodBase.ToString 代替它(你怎么看?)

此外,我们有 BenchmarkCall 类,它允许我们注意调用完成了多少次以及花费了多少时间:

    [Serializable]
    public class BenchmarkCall
    {
        public string Method { get; set; }

        public double SpentMilliseconds { get; set; }
        public long SpentTicks { get; set; }
        public double MinSpentMs { get; set; } = double.MaxValue;
        public double MaxSpentMs { get; set; } = double.MinValue;
        public long MinSpentTicks { get; set; } = long.MaxValue;
        public long MaxSpentTicks { get; set; } = long.MinValue;
        public double AvgMs => SpentMilliseconds / TimesCalled;
        public double AvgTicks => SpentTicks / (double)TimesCalled;

        public BenchmarkCall()
        {
        }

        public BenchmarkCall(MethodBase method)
        {
            Method = method.GetMethodSignature();
        }

        public override string ToString()
        {
            if (TimesCalled > 0)
                return "BenchmarkCall{\n" +
                       $"Ticks[SpentTicks={SpentTicks},MinTicks={MinSpentTicks},MaxTicks={MaxSpentTicks},AvgTicks={AvgTicks:F2}]\n" +
                       $"Ms[SpentMs={SpentMilliseconds:F2},MinMs={MinSpentMs:F2},MaxMs={MaxSpentMs:F2},AvgMs={AvgMs:F2}]\n" +
                       "}";

            return "BenchmarkCall{}";
        }
    }
}

所以我认为我的下一个动作是区分从 A 或 B(Xa 或 Xb)调用的 X 方法来处理完整的层次结构(我不知道该怎么做)而不是父方法调用它,也许我写的代码与它有关,但我不确定(昨晚我太累了,所以我没有编写代码来照顾这些事实),建立一个方法列表具有不同ID的签名,然后填充树,ID 1是Xa,ID 2是Xb(我在填充树时也有问题)。

此外,我还需要使用 Transpiler 来更改所有代码指令,因此如果一个方法具有:

void method() {
 X1();
 X2();
}

我们需要添加 2 个方法(如前缀/后缀)来测量每个指令调用:

void method() {
 Start(1);
 X1();
 End(1);

 Start(2);
 X2();
 End(2);
}

这将是一项艰巨的任务,但我希望有人能指导我解决这个问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res