我听说这个术语在与日志记录相同的上下文中使用了很多,但我似乎无法找到它实际上是什么的明确定义。
它只是一种更通用的日志/监控工具和活动吗?
请提供示例代码/场景何时/如何使用仪器。
我听说这个术语在与日志记录相同的上下文中使用了很多,但我似乎无法找到它实际上是什么的明确定义。
它只是一种更通用的日志/监控工具和活动吗?
请提供示例代码/场景何时/如何使用仪器。
I write tools that perform instrumentation. So here is what I think it is.
DLL rewriting. This is what tools like Purify and Quantify do. A previous reply to this question said that they instrument post-compile/link. That is not correct. Purify and Quantify instrument the DLL the first time it is executed after a compile/link cycle, then cache the result so that it can be used more quickly next time around. For large applications, profiling the DLLs can be very time consuming. It is also problematic - at a company I worked at between 1998-2000 we had a large 2 million line app that would take 4 hours to instrument, and 2 of the DLLs would randomly crash during instrumentation and if either failed you would have do delete both of them, then start over.
In place instrumentation. This is similar to DLL rewriting, except that the DLL is not modified and the image on the disk remains untouched. The DLL functions are hooked appropriately to the task required when the DLL is first loaded (either during startup or after a call to LoadLibrary(Ex). You can see techniques similar to this in the Microsoft Detours library.
On-the-fly instrumentation. Similar to in-place but only actually instruments a method the first time the method is executed. This is more complex than in-place and delays the instrumentation penalty until the first time the method is encountered. Depending on what you are doing, that could be a good thing or a bad thing.
Intermediate language instrumentation. This is what is often done with Java and .Net languages (C~, VB.Net, F#, etc). The language is compiled to an intermediate language which is then executed by a virtual machine. The virtual machine provides an interface (JVMTI for Java, ICorProfiler(2) for .Net) which allows you to monitor what the virtual machine is doing. Some of these options allow you to modify the intermediate language just before it gets compiled to executable instructions.
Intermediate language instrumentation via reflection. Java and .Net both provide reflection APIs that allow the discovery of metadata about methods. Using this data you can create new methods on the fly and instrument existing methods just as with the previously mentioned Intermediate language instrumentation.
Compile time instrumentation. This technique is used at compile time to insert appropriate instructions into the application during compilation. Not often used, a profiling feature of Visual Studio provides this feature. Requires a full rebuild and link.
Source code instrumentation. This technique is used to modify source code to insert appropriate code (usually conditionally compiled so you can turn it off).
Link time instrumentation. This technique is only really useful for replacing the default memory allocators with tracing allocators. An early example of this was the Sentinel memory leak detector on Solaris/HP in the early 1990s.
The various in-place and on-the-fly instrumentation methods are fraught with danger as it is very hard to stop all threads safely and modify the code without running the risk of requiring an API call that may want to access a lock which is held by a thread you've just paused - you don't want to do that, you'll get a deadlock. You also have to check if any of the other threads are executing that method, because if they are you can't modify it.
The virtual machine based instrumentation methods are much easier to use as the virtual machine guarantees that you can safely modify the code at that point.
I think I've covered them all, hope that helps.
这是一个通用术语,用于对您的代码进行一些进一步分析所必需的操作。
特别是对于像 C 或 C++ 这样的语言,有像 Purify 或 Quantify 这样的工具可以分析内存使用情况、性能统计信息等。为了使这些分析程序正常工作,需要一个“检测”步骤来插入分析程序使用的计数器、数组边界检查等。请注意,在 Purify/Quantify 场景中,检测是作为编译后步骤自动完成的(实际上,它是链接过程的附加步骤),并且您无需接触源代码。
其中一些对于动态或 VM 代码是不必要的(即像 OptimizeIt 这样的分析工具可用于 Java,它可以完成 Quantify 所做的很多工作,但不需要特殊的链接),但这并不否定这个概念。
检测通常用于动态代码分析。
它与日志记录不同,因为检测通常由软件自动完成,而日志记录需要人类智能来插入日志记录代码。
维基百科文章的摘录
在计算机编程的上下文中,仪表是指监控或测量产品性能水平、诊断错误和写入跟踪信息的能力。程序员以监控系统中特定组件的代码指令的形式实现检测(例如,指令可以输出日志信息以显示在屏幕上)。当应用程序包含检测代码时,可以使用管理工具对其进行管理。检测对于审查应用程序的性能是必要的。检测方法可以有两种类型,源检测和二进制检测。
无论 Wikipedia 怎么说,IT 行业中的代码检测都没有标准/广泛认可的定义。
请考虑一下,instrumentation是从instrument派生的名词,具有非常广泛的含义。
“代码”也是 IT 中的一切,我的意思是——数据、服务,一切。
因此,代码检测是一组如此广泛的应用程序......不值得给它一个单独的名称;-)。
这可能就是为什么这篇维基百科文章只是一个存根的原因。