Patch by Bjoern Herzig, adds -a option, some cleanup.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@29266 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+103
-46
@@ -5,30 +5,103 @@
|
|||||||
* Authors:
|
* Authors:
|
||||||
* Francois Revol (mmu_man)
|
* Francois Revol (mmu_man)
|
||||||
* Salvatore Benedetto <[email protected]>
|
* Salvatore Benedetto <[email protected]>
|
||||||
|
* Bjoern Herzig (xRaich[o]2x)
|
||||||
*/
|
*/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <OS.h>
|
#include <OS.h>
|
||||||
|
|
||||||
#define SNOOZE_TIME 100000
|
#define SNOOZE_TIME 100000
|
||||||
|
|
||||||
|
char *states[] = {"run", "rdy", "msg", "zzz", "sus", "wait" };
|
||||||
|
|
||||||
int
|
void printTeamThreads(team_info *teamInfo, bool printSemaphoreInfo);
|
||||||
main(int argc, char **argv)
|
void printTeamInfo(team_info *teamInfo, bool printHeader);
|
||||||
|
|
||||||
|
void printTeamInfo(team_info *teamInfo, bool printHeader)
|
||||||
{
|
{
|
||||||
team_info teamInfo;
|
// Print team info
|
||||||
thread_info threadInfo;
|
if (printHeader)
|
||||||
uint32 teamCookie = 0;
|
printf("%-49s %5s %8s %4s %4s\n", "Team", "Id", "#Threads", "Gid", "Uid");
|
||||||
|
|
||||||
|
printf("%-49s %5ld %8ld %4d %4d\n", teamInfo->args, teamInfo->team,
|
||||||
|
teamInfo->thread_count, teamInfo->uid, teamInfo->gid);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printTeamThreads(team_info *teamInfo, bool printSemaphoreInfo) {
|
||||||
|
char *threadState;
|
||||||
uint32 threadCookie = 0;
|
uint32 threadCookie = 0;
|
||||||
sem_info semaphoreInfo;
|
sem_info semaphoreInfo;
|
||||||
char *threadState;
|
thread_info threadInfo;
|
||||||
char *states[] = {"run", "rdy", "msg", "zzz", "sus", "wait" };
|
|
||||||
|
// Print all info about its threads too
|
||||||
|
while (get_next_thread_info(teamInfo->team, &threadCookie, &threadInfo)
|
||||||
|
>= B_OK) {
|
||||||
|
if (threadInfo.state < B_THREAD_RUNNING
|
||||||
|
|| threadInfo.state > B_THREAD_WAITING)
|
||||||
|
// This should never happen
|
||||||
|
threadState = "???";
|
||||||
|
else
|
||||||
|
threadState = states[threadInfo.state - 1];
|
||||||
|
|
||||||
|
printf("%-29s %5ld %8s %4ld %8llu %8llu ",
|
||||||
|
threadInfo.name, threadInfo.thread, threadState,
|
||||||
|
threadInfo.priority, (threadInfo.user_time / 1000),
|
||||||
|
(threadInfo.kernel_time / 1000));
|
||||||
|
|
||||||
|
if (printSemaphoreInfo) {
|
||||||
|
if (threadInfo.state == B_THREAD_WAITING && threadInfo.sem != -1) {
|
||||||
|
status_t status = get_sem_info(threadInfo.sem, &semaphoreInfo);
|
||||||
|
if (status == B_OK)
|
||||||
|
printf("%s(%ld)\n", semaphoreInfo.name, semaphoreInfo.sem);
|
||||||
|
else
|
||||||
|
printf("%s(%ld)\n", strerror(status), threadInfo.sem);
|
||||||
|
} else
|
||||||
|
puts("");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
puts("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
team_info teamInfo;
|
||||||
|
uint32 teamCookie = 0;
|
||||||
system_info systemInfo;
|
system_info systemInfo;
|
||||||
bool printSystemInfo = true;
|
bool printSystemInfo = false;
|
||||||
|
bool printThreads = false;
|
||||||
|
bool printHeader = true;
|
||||||
|
bool printSemaphoreInfo = false;
|
||||||
// match this in team name
|
// match this in team name
|
||||||
char *string_to_match;
|
char *string_to_match;
|
||||||
|
|
||||||
|
int c;
|
||||||
|
|
||||||
|
while ((c = getopt(argc, argv,"ihas")) != EOF) {
|
||||||
|
switch(c) {
|
||||||
|
case 'i':
|
||||||
|
printSystemInfo = true;
|
||||||
|
break;
|
||||||
|
case 'h':
|
||||||
|
printf( "usage: ps [-hais] [team]\n"
|
||||||
|
"-h : show help\n"
|
||||||
|
"-i : show system info\n"
|
||||||
|
"-s : show semaphore info\n"
|
||||||
|
"-a : show threads too (by default only teams are displayed)\n");
|
||||||
|
return 0;
|
||||||
|
break;
|
||||||
|
case 'a':
|
||||||
|
printThreads = true;
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
printSemaphoreInfo = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: parse command line
|
// TODO: parse command line
|
||||||
// Possible command line options:
|
// Possible command line options:
|
||||||
// -h show help
|
// -h show help
|
||||||
@@ -38,11 +111,27 @@ main(int argc, char **argv)
|
|||||||
// -a show threads too (by default only teams are displayed)
|
// -a show threads too (by default only teams are displayed)
|
||||||
// -s show semaphore info
|
// -s show semaphore info
|
||||||
// -i show system info
|
// -i show system info
|
||||||
string_to_match = (argc == 2) ? argv[1] : NULL;
|
|
||||||
|
|
||||||
printf("%-50s %4s %8s %4s %4s\n", "Team", "Id", "#Threads", "Gid", "Uid");
|
if (argc == 2 && (printSystemInfo||printThreads))
|
||||||
|
string_to_match = NULL;
|
||||||
|
else
|
||||||
|
string_to_match = (argc >= 2) ? argv[argc-1] : NULL;
|
||||||
|
|
||||||
|
if (!string_to_match) {
|
||||||
|
while (get_next_team_info(&teamCookie, &teamInfo) >= B_OK) {
|
||||||
|
|
||||||
|
printTeamInfo(&teamInfo,printHeader);
|
||||||
|
printHeader = false;
|
||||||
|
if (printThreads) {
|
||||||
|
printf("\n%-29s %5s %8s %4s %8s %8s\n", "Thread", "Id", "State","Prio", "UTime", "KTime");
|
||||||
|
printTeamThreads(&teamInfo,printSemaphoreInfo);
|
||||||
|
printf("--------------------------------------------------------------------------\n");
|
||||||
|
printHeader = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
while (get_next_team_info(&teamCookie, &teamInfo) >= B_OK) {
|
while (get_next_team_info(&teamCookie, &teamInfo) >= B_OK) {
|
||||||
if (string_to_match) {
|
|
||||||
char *p;
|
char *p;
|
||||||
p = teamInfo.args;
|
p = teamInfo.args;
|
||||||
if ((p = strchr(p, ' ')))
|
if ((p = strchr(p, ' ')))
|
||||||
@@ -52,41 +141,9 @@ main(int argc, char **argv)
|
|||||||
p = teamInfo.args;
|
p = teamInfo.args;
|
||||||
if (strstr(p, string_to_match) == NULL)
|
if (strstr(p, string_to_match) == NULL)
|
||||||
continue;
|
continue;
|
||||||
// Print team info
|
printTeamInfo(&teamInfo,true);
|
||||||
printf("%-50s %4ld %8ld %4d %4d\n\n", teamInfo.args, teamInfo.team,
|
printf("\n%-29s %5s %8s %4s %8s %8s\n", "Thread", "Id", "State","Prio", "UTime", "KTime");
|
||||||
teamInfo.thread_count, teamInfo.uid, teamInfo.gid);
|
printTeamThreads(&teamInfo,printSemaphoreInfo);
|
||||||
|
|
||||||
printf("%-30s %4s %8s %4s %8s %8s\n", "Thread", "Id", "State",
|
|
||||||
"Prio", "UTime", "KTime");
|
|
||||||
// Print all info about its threads too
|
|
||||||
while (get_next_thread_info(teamInfo.team, &threadCookie, &threadInfo)
|
|
||||||
>= B_OK) {
|
|
||||||
if (threadInfo.state < B_THREAD_RUNNING
|
|
||||||
|| threadInfo.state > B_THREAD_WAITING)
|
|
||||||
// This should never happen
|
|
||||||
threadState = "???";
|
|
||||||
else
|
|
||||||
threadState = states[threadInfo.state - 1];
|
|
||||||
|
|
||||||
printf("%-30s %4ld %8s %4ld %8llu %8llu ",
|
|
||||||
threadInfo.name, threadInfo.thread, threadState,
|
|
||||||
threadInfo.priority, (threadInfo.user_time / 1000),
|
|
||||||
(threadInfo.kernel_time / 1000));
|
|
||||||
|
|
||||||
if (threadInfo.state == B_THREAD_WAITING && threadInfo.sem != -1) {
|
|
||||||
status_t status = get_sem_info(threadInfo.sem, &semaphoreInfo);
|
|
||||||
if (status == B_OK)
|
|
||||||
printf("%s(%ld)\n", semaphoreInfo.name, semaphoreInfo.sem);
|
|
||||||
else
|
|
||||||
printf("%s(%ld)\n", strerror(status), threadInfo.sem);
|
|
||||||
} else
|
|
||||||
puts("");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
printf("%-50s %4ld %8ld %4d %4d\n", teamInfo.args, teamInfo.team,
|
|
||||||
teamInfo.thread_count, teamInfo.uid, teamInfo.gid);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user