Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Add pstatus json format output #982

Merged
merged 5 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docs/Command/Pstatus.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
# Synopsis

~~~
bash$ gridlabd --pstatus
bash$ gridlabd --pstatus[=json]
~~~

# Description

Prints the process list.

Prints the process list. If `=json` is included the output is formatted as a JSON file. Otherwise the output is in tabular form.
6 changes: 5 additions & 1 deletion gldcore/cmdarg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,11 @@ DEPRECATED static int pstatus(void *main, int argc, const char *argv[])
int GldCmdarg::pstatus(int argc, const char *argv[])
{
sched_init(1);
sched_print(0);
const char *opt = strchr(argv[0],'=');
if ( opt != NULL )
sched_print(0,opt+1);
else
sched_print(0);
return 0;
}

Expand Down
73 changes: 58 additions & 15 deletions gldcore/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2033,7 +2033,7 @@ int sched_getnproc(void)
return n_procs;
}

void sched_print(int flags) /* flag=0 for single listing, flag=1 for continuous listing */
void sched_print(int flags, const char* format) /* flag=0 for single listing, flag=1 for continuous listing */
{
char line[1024];
int width = 80, namesize;
Expand All @@ -2057,24 +2057,67 @@ void sched_print(int flags) /* flag=0 for single listing, flag=1 for continuous
if ( namesize>1024 ) namesize=1024;
if ( name!=NULL ) free(name);
name = (char*)malloc(namesize+1);
if ( process_map!=NULL )
if ( flags == 1 && format != NULL )
{
unsigned int n;
if ( flags==1 )
{
sched_getinfo(-1,line,sizeof(line)-1);
printf("%s\n",line);
sched_getinfo(-2,line,sizeof(line)-1);
printf("%s\n",line);
output_error("pstatus format '%s' not supported for continuous output",format);
}
else if ( process_map != NULL )
{
if ( format != NULL && strcmp(format,"json") == 0 )
{
unsigned int n;
printf("{\n");
for ( n=0 ; n<n_procs ; n++ )
{
PROCINFO pinfo;
if ( sched_getinfo(n,&pinfo) == SUCCESS )
{
printf(" \"%d\" : {\n",n);
sched_lock(n);
char buffer[64];
printf(" \"pid\" : %d,\n",process_map[n].pid);
printf(" \"progress\" : \"%s\",\n",convert_from_timestamp(process_map[n].progress,buffer,sizeof(buffer))>0?buffer:"");
printf(" \"starttime\" : \"%s\",\n",convert_from_timestamp(process_map[n].starttime,buffer,sizeof(buffer))>0?buffer:"");
printf(" \"stoptime\" : \"%s\",\n",convert_from_timestamp(process_map[n].stoptime,buffer,sizeof(buffer))>0?buffer:"");
const char *status = NULL;
switch ( process_map[n].status )
{
case MLS_INIT: status = "INIT"; break;
case MLS_RUNNING: status = "RUNNING"; break;
case MLS_PAUSED: status = "PAUSED"; break;
case MLS_DONE: status = "DONE"; break;
case MLS_LOCKED: status = "LOCKED"; break;
default: status = "UNKNOWN"; break;
}
printf(" \"status\" : \"%s\",\n",status);
printf(" \"model\" : \"%s\",\n",(const char*)process_map[n].model);
printf(" \"start\" : \"%s\",\n",convert_from_timestamp(process_map[n].start,buffer,sizeof(buffer))>0?buffer:"");
printf(" \"port\" : %d\n",process_map[n].port);
sched_unlock(n);
printf(" }%s\n",n<n_procs-1?",":"");
}
}
printf("}\n");
}
for ( n=0 ; n<n_procs ; n++ )
else
{
if ( process_map[n].pid!=0 || flags==1 )
unsigned int n;
if ( flags==1 )
{
if ( sched_getinfo(n,line,sizeof(line)-1)>0 )
printf("%s\n",line);
else
printf("%4d (error)\n",n);
sched_getinfo(-1,line,sizeof(line)-1);
printf("%s\n",line);
sched_getinfo(-2,line,sizeof(line)-1);
printf("%s\n",line);
}
for ( n=0 ; n<n_procs ; n++ )
{
if ( process_map[n].pid!=0 || flags==1 )
{
if ( sched_getinfo(n,line,sizeof(line)-1)>0 )
printf("%s\n",line);
else
printf("%4d (error)\n",n);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion gldcore/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ extern "C" {

void sched_init(int readonly);
void sched_clear(void);
void sched_print(int flags);
void sched_print(int flags, const char *format = NULL);
void sched_update(TIMESTAMP clock, enumeration status);
void sched_pkill(pid_t pid, int signal=SIGINT);
void sched_controller(void);
Expand Down