src/share/bin/java.c::SetJvmEnvironment
/*
* static void SetJvmEnvironment(int argc, char **argv);
* Is called just before the JVM is loaded. We can set env variables
* that are consumed by the JVM. This function is non-destructive,
* leaving the arg list intact. The first use is for the JVM flag
* -XX:NativeMemoryTracking=value.
*/
static void
SetJvmEnvironment(int argc, char **argv) {
static const char* NMT_Env_Name = "NMT_LEVEL_";
int i;
for (i = 0; i < argc; i++) {
/*
* The following case checks for "-XX:NativeMemoryTracking=value".
* If value is non null, an environmental variable set to this value
* will be created to be used by the JVM.
* The argument is passed to the JVM, which will check validity.
* The JVM is responsible for removing the env variable.
*/
char *arg = argv[i];
//判断命令行参数是否包含:-XX:NativeMemoryTracking=
//如果是,根据值设置环境变量:NMT_LEVEL_${PID}=value,用以内存跟踪
if (JLI_StrCCmp(arg, "-XX:NativeMemoryTracking=") == 0) {
int retval;
// get what follows this parameter, include "="
size_t pnlen = JLI_StrLen("-XX:NativeMemoryTracking=");
if (JLI_StrLen(arg) > pnlen) {
//获取参数值
char* value = arg + pnlen;
size_t pbuflen = pnlen + JLI_StrLen(value) + 10; // 10 max pid digits
/*
* ensures that malloc successful
* DONT JLI_MemFree() pbuf. JLI_PutEnv() uses system call
* that could store the address.
*/
char * pbuf = (char*)JLI_MemAlloc(pbuflen);
//拼接环境变量到pbuf中
JLI_Snprintf(pbuf, pbuflen, "%s%d=%s", NMT_Env_Name, JLI_GetPid(), value);
//设置环境变量,成功返回0,发生错误返回-1
retval = JLI_PutEnv(pbuf);
if (JLI_IsTraceLauncher()) {
char* envName;
char* envBuf;
// ensures that malloc successful
envName = (char*)JLI_MemAlloc(pbuflen);
JLI_Snprintf(envName, pbuflen, "%s%d", NMT_Env_Name, JLI_GetPid());
//TRACER_MARKER: NativeMemoryTracking: env var is NMT_LEVEL_11380
printf("TRACER_MARKER: NativeMemoryTracking: env var is %s\n",envName);
//TRACER_MARKER: NativeMemoryTracking: putenv arg NMT_LEVEL_11380=summary
printf("TRACER_MARKER: NativeMemoryTracking: putenv arg %s\n",pbuf);
envBuf = getenv(envName);
//TRACER_MARKER: NativeMemoryTracking: got value summary
printf("TRACER_MARKER: NativeMemoryTracking: got value %s\n",envBuf);
free(envName);
}
}
}
}
}