Refactor LinuxProcessList_readSmapsFile to work line-oriented

This commit is contained in:
Benny Baumann 2020-10-27 19:30:05 +01:00 committed by cgzones
parent e89b289494
commit cf1a9ec180
1 changed files with 24 additions and 35 deletions

View File

@ -479,54 +479,43 @@ static bool LinuxProcessList_readSmapsFile(LinuxProcess* process, const char* di
//http://elixir.free-electrons.com/linux/v4.10/source/fs/proc/task_mmu.c#L719 //http://elixir.free-electrons.com/linux/v4.10/source/fs/proc/task_mmu.c#L719
//kernel will return data in chunks of size PAGE_SIZE or less. //kernel will return data in chunks of size PAGE_SIZE or less.
char buffer[CRT_pageSize];// 4k char buffer[256];
ssize_t nread=0;
if(haveSmapsRollup) {// only available in Linux 4.14+ if(haveSmapsRollup) {// only available in Linux 4.14+
xSnprintf(buffer, sizeof(buffer), "%s/%s/smaps_rollup", dirname, name); xSnprintf(buffer, sizeof(buffer), "%s/%s/smaps_rollup", dirname, name);
} else { } else {
xSnprintf(buffer, sizeof(buffer), "%s/%s/smaps", dirname, name); xSnprintf(buffer, sizeof(buffer), "%s/%s/smaps", dirname, name);
} }
int fd = open(buffer, O_RDONLY);
if (fd == -1) FILE* f = fopen(buffer, "r");
if (!f)
return false; return false;
process->m_pss = 0; process->m_pss = 0;
process->m_swap = 0; process->m_swap = 0;
process->m_psswp = 0; process->m_psswp = 0;
while ( ( nread = read(fd,buffer, sizeof(buffer)) ) > 0 ){ while (fgets(buffer, sizeof(buffer), f)) {
char* start = buffer; if(!strchr(buffer, '\n')) {
char* end = start + nread; // Partial line, skip to end of this line
do{//parse 4k block while(fgets(buffer, sizeof(buffer), f)) {
int tmp; if(strchr(buffer, '\n')) {
break;
}
}
continue;
}
if( (tmp = (end - start)) > 0 && if (String_startsWith(buffer, "Pss:")) {
(start = memmem(start,tmp,"\nPss:",5)) != NULL ) process->m_pss += strtol(buffer + 4, NULL, 10);
{ } else if (String_startsWith(buffer, "Swap:")) {
process->m_pss += strtol(start+5, &start, 10); process->m_swap += strtol(buffer + 5, NULL, 10);
start += 3;//now we must be at the end of line "Pss: 0 kB" } else if (String_startsWith(buffer, "SwapPss:")) {
}else process->m_psswp += strtol(buffer + 8, NULL, 10);
break; //read next 4k block }
}
if( (tmp = (end - start)) > 0 && fclose(f);
(start = memmem(start,tmp,"\nSwap:",6)) != NULL )
{
process->m_swap += strtol(start+6, &start, 10);
start += 3;
}else
break;
if( (tmp = (end - start)) > 0 &&
(start = memmem(start,tmp,"\nSwapPss:",9)) != NULL )
{
process->m_psswp += strtol(start+9, &start, 10);
start += 3;
}else
break;
}while(1);
}//while read
close(fd);
return true; return true;
} }