Software Archive
Read-only legacy content
17061 Discussions

Check if a process is running on the Intel Phi

Jeremie_Lagraviere
353 Views

Hi everyone,

For energy measurement purposes I need to run a program "X" on the Intel phi, that detects when a program "Y" is running in the Intel phi.

Program X runs natively on the Intel Phi (I launch it directly from an ssh terminal logged in the Intel Phi) while program Y is offloaded from the host using micnativeloadex

Here is the code I use in program X, to find if a process is running is a function, that takes in parameter the name of the process:

int findProcessByName(const char* name) 
{
	DIR* dir;
	struct dirent* ent;
	char* endptr;
	char buf[512];

	//Can we open /proc directory ?
	if (!(dir = opendir("/proc")))
	{
		perror("can't open /proc");
		return -1;
	}

	while((ent = readdir(dir)) != NULL)
	{
		/* if endptr is not a null character, the directory is not
		* entirely numeric, so ignore it */
		long lpid = strtol(ent->d_name, &endptr, 10);
		if (*endptr != '\0')
		{
			continue;
		}


		/* try to open the cmdline file */
		snprintf(buf, sizeof(buf), "/proc/%ld/cmdline", lpid);
		FILE* fp = fopen(buf, "r");

		if (fp)
		{
			if (fgets(buf, sizeof(buf), fp) != NULL)
			{
				/* check the first token in the file, the program name */
				char* first = strtok(buf, " ");
				if (!strcmp(first, name))
				{
					fclose(fp);
					closedir(dir);
					return 1;
				}
			}

			fclose(fp);
		}
	}

	closedir(dir);
	return -1;
}

However I am guessing that the problem comes from the fact the processes running on the Phi are not present at this location "/proc/[PID]/cmdline" ?

Because, this function above, works perfectly on a "regular computer" (the host of the Intel Phi for example).

But it does not work on the Intel Phi.

I am guessing that the directory where I should look is "/tmp/coi_procs/1" am I right ? Is it the right place to look for process running on the Intel Phi? 

 

Thanks in advance for your help

0 Kudos
4 Replies
Frances_R_Intel
Employee
353 Views

1) The name of the offload process running on the coprocessor is not exactly the same as the name of the process running on the host. If you want to compare the name, get the length of the name on the host and check only that many characters against the name on the coprocessor.

2) The offload process on the coprocessor is forked off by the coi_daemon and runs under the same user name as the user name running the coi_daemon (micuser by default). Might it be easier to check for when the coi_daemon forks a process.

 

0 Kudos
Jeremie_Lagraviere
353 Views

Thanks for the hints.

However I have no idea on how to implement (2)

About (1): when I run the  "top" command I can clearly see my program "Y" in the process list. So far it seems to be called the same as on the host. This is actually why I was guessing that maybe my program X is not looking in the right place for the process...

0 Kudos
JJK
New Contributor III
353 Views

read /proc/$PID/status instead; it contains an entry Name (first line, usually) which lists the name of the process. This is also what the 'top' command uses, as 'top' lists kernel processes that have no cmdline (neither on the Phi nor on the host).


 

0 Kudos
Frances_R_Intel
Employee
353 Views

The source code for the coi_daemon is included in the mpss-3.5.1-k1om.tar file that comes with the MPSS. If you have a sudden burst of excess programming energy, you could always add a print statement to the daemon to print out the new child pid when it gets created by the daemon - or if you have a really major sudden burst of excess programming energy, you could even set up a pipe between your program and the daemon.

However, since your goal is probably to make life simpler, rather than harder, if you are happy with the information that JJK has provided, then I am happy. Alternately, you could watch for the working directory for your offload process to be make - which I believe is still /tmp/coi_procs/1/${pid} by default.

0 Kudos
Reply