Skip to content

Commit 097a4e4

Browse files
various improvements, cpu percentages, human readable file sizes in dir
1 parent 982f942 commit 097a4e4

11 files changed

Lines changed: 150 additions & 50 deletions

File tree

include/basic/process.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,5 @@ void eval_statement(struct basic_ctx* ctx);
133133
void goto_statement(struct basic_ctx* ctx);
134134

135135
void yield_statement(struct basic_ctx* ctx);
136+
137+
int64_t basic_getproccpu(struct basic_ctx* ctx);

include/taskswitch.h

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,24 @@ typedef bool (*activity_callback_t)(struct process_t* proc, void* opaque);
5353
* in a global doubly-linked list for scheduling.
5454
*/
5555
typedef struct process_t {
56-
pid_t pid; /**< Unique process ID */
57-
pid_t ppid; /**< Parent process ID */
58-
process_state_t state; /**< Running state */
59-
time_t start_time; /**< Start time (UNIX epoch) */
60-
pid_t waitpid; /**< PID being waited on */
61-
cpu_id_t cpu; /**< Logical CPU this process is assigned to */
62-
const char* directory; /**< Directory of program */
63-
const char* name; /**< Filename of program */
64-
size_t size; /**< Size of program in bytes */
65-
const char* csd; /**< Current selected directory */
66-
struct basic_ctx* code; /**< BASIC interpreter context */
67-
struct process_t* sched_next; /**< Next process in doubly linked list */
68-
struct process_t* sched_prev; /**< Previous process in doubly linked list */
69-
struct process_t* global_next; /**< Next process in doubly linked list */
70-
struct process_t* global_prev; /**< Previous process in doubly linked list */
71-
activity_callback_t check_idle; /**< If non-null, called to check if the process should remain idle */
72-
void* idle_context; /**< Opaque context passed to the check_idle callback */
56+
pid_t pid; /**< Unique process ID */
57+
pid_t ppid; /**< Parent process ID */
58+
process_state_t state; /**< Running state */
59+
time_t start_time; /**< Start time (UNIX epoch) */
60+
pid_t waitpid; /**< PID being waited on */
61+
cpu_id_t cpu; /**< Logical CPU this process is assigned to */
62+
const char* directory; /**< Directory of program */
63+
const char* name; /**< Filename of program */
64+
size_t size; /**< Size of program in bytes */
65+
const char* csd; /**< Current selected directory */
66+
struct basic_ctx* code; /**< BASIC interpreter context */
67+
struct process_t* sched_next; /**< Next process in doubly linked list */
68+
struct process_t* sched_prev; /**< Previous process in doubly linked list */
69+
struct process_t* global_next; /**< Next process in doubly linked list */
70+
struct process_t* global_prev; /**< Previous process in doubly linked list */
71+
activity_callback_t check_idle; /**< If non-null, called to check if the process should remain idle */
72+
void* idle_context; /**< Opaque context passed to the check_idle callback */
73+
uint32_t cpu_percent; /**< Rolling average CPU usage percentage */
7374
} process_t;
7475

7576
/**
@@ -305,4 +306,31 @@ void proc_queue_dpc(dpc_t handler);
305306
*/
306307
void run_idles(uint8_t cpu);
307308

308-
process_t* proc_load_anonymous(const char* source, pid_t parent_pid, const char* csd);
309+
/**
310+
* @brief Create and start an anonymous BASIC process from a source string
311+
*
312+
* This function creates a new process using the provided BASIC source code
313+
* rather than loading it from the filesystem. The process is initialised
314+
* and scheduled in the same way as one created via proc_load().
315+
*
316+
* @param source The BASIC program source code (must be valid and non-empty)
317+
* @param parent_pid The PID of the parent process
318+
* @param csd The current working directory to assign to the process
319+
*
320+
* @return Pointer to the newly created process on success, or NULL on failure
321+
*/
322+
process_t* proc_load_anonymous(const char* source, pid_t parent_pid, const char* csd);
323+
324+
/**
325+
* @brief Get the averaged CPU usage percentage for a process
326+
*
327+
* Returns the rolling average CPU usage for the specified process as an
328+
* integer percentage in the range 0–100. A value of 0 indicates the process
329+
* is idle or waiting, while higher values indicate a greater share of CPU
330+
* time over the recent sampling window.
331+
*
332+
* @param pid The process ID to query
333+
*
334+
* @return CPU usage percentage for the process, or 0 if the PID is invalid
335+
*/
336+
uint32_t proc_cpu_percent(pid_t pid);

os/programs/charmap.rrbasic

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
FOR C = &20 TO &7E
2-
COLOR 8
2+
COLOUR 8
33
PRINT ~C; ": ";
4-
COLOR 15
4+
COLOUR 15
55
PRINT CHR$(C); " ";
66
IF C % 8 = 7 THEN PRINT ""
77
NEXT
88
PRINT ""
9-
COLOR 7
9+
COLOUR 7

os/programs/dir.rrbasic

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,35 @@ IF DIR$ <> "" THEN PREPEND$ = DIR$ + "/"
99

1010
FOR X = N - 1 TO 0 STEP -1
1111
FILE$ = GETNAME$(DIR$,X)
12-
IF FILETYPE$(PREPEND$ + FILE$) = "directory" THEN COLOR 9
13-
IF FILETYPE$(PREPEND$ + FILE$) = "file" THEN COLOR 15
12+
FT$ = FILETYPE$(PREPEND$ + FILE$)
13+
IF FT$ = "directory" THEN COLOUR 9
14+
IF FT$ = "file" THEN COLOUR 15
1415
PRINT FILE$;
1516
COLOUR 8
1617
REPEAT
1718
PRINT " ";
1819
UNTIL CURRENTX > 25
19-
SIZE# = GETSIZE(DIR$,X)
20-
PRINT SIZE#;
20+
PRINT FNhumanSize$(GETSIZE(DIR$,X));
2121
REPEAT
2222
PRINT " ";
2323
UNTIL CURRENTX > 35
24-
COLOR 7
25-
PRINT UPPER$(LEFT$(FILETYPE$(PREPEND$ + FILE$),1))
24+
COLOUR 7
25+
PRINT UPPER$(LEFT$(FT$,1))
2626
NEXT
2727
PROCreport
2828
END
2929

3030
DEF PROCreport
3131
IF DIR$ = "" THEN DIR$ = CSD$
32-
COLOR 7
32+
COLOUR 7
3333
PRINT N; " items in " + DIR$
3434
ENDPROC
35+
36+
DEF FNhumanSize$(size)
37+
X$ = STR$(size)
38+
IF (size > 1024 * 1024) THEN
39+
X$ = STR$(size / 1024 / 1024) + "M"
40+
size = 0
41+
ENDIF
42+
IF (size > 1024) THEN X$ = STR$(size / 1024) + "K"
43+
=X$

os/programs/init.rrbasic

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ UNTIL FALSE
2727

2828
DEF PROCmessage(white$, yellow$)
2929
PRINT white$; " ";
30-
COLOR 14
30+
COLOUR 14
3131
PRINT yellow$;
32-
COLOR 7
32+
COLOUR 7
3333
PRINT "..."
3434
ENDPROC
3535

os/programs/irc.rrbasic

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ ENDPROC
9797
DEF PROCdisplayInput
9898
PROChideCursor
9999
CURSOR 2, TERMHEIGHT
100-
COLOR 15
100+
COLOUR 15
101101
BACKGROUND 1
102102
leftX = LEN(curChannel$) + 5
103103
PRINT "["; curChannel$; "] ";

os/programs/proclist.rrbasic

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
REM Process List
22
REM ------------
33

4-
COLOR 15
5-
PRINT "PID", "PPID", "CPUID", "MEMORY", "STATE ", "PROCESS NAME"
6-
COLOR 7
4+
COLOUR 15
5+
PRINT "PID", "PPID", "CPUID", "CPU%", "MEMORY", "STATE ", "PROCESS NAME"
6+
COLOUR 7
77
PROCline()
8+
IDLE = 100
89
FOR Y = 0 TO GETPROCCOUNT - 1
9-
PRINT GETPROCID(Y), GETPROCPARENT(Y); " ", GETPROCCPUID(Y), FNhumanSize$(GETPROCMEM(Y)), GETPROCSTATE$(Y); " ";
10-
COLOR 15
10+
PERC = GETPROCCPU(GETPROCID(Y))
11+
IDLE = IDLE - PERC
12+
PRINT GETPROCID(Y), GETPROCPARENT(Y); " ", GETPROCCPUID(Y), PERC; "%", FNhumanSize$(GETPROCMEM(Y)), GETPROCSTATE$(Y); " ";
13+
COLOUR 15
1114
PRINT "", LEFT$(GETPROCNAME$(Y), 60)
12-
COLOR 7
15+
COLOUR 7
1316
NEXT
17+
PRINT "-", "-"; " ", "-", MAX(IDLE, 0); "%", "0K", "idle ";
18+
COLOUR 15
19+
PRINT "", "Idle"
20+
COLOUR 7
1421
PROCline()
15-
COLOR 15
22+
COLOUR 15
1623
PRINT GETPROCCOUNT; " Total Processes"
17-
COLOR 7
24+
COLOUR 7
1825
END
1926

2027
DEF FNhumanSize$(size)

os/programs/rocketsh.rrbasic

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ VERSION$ = "3.1"
1414
REM Welcome message
1515
PRINT ""
1616
PRINT "Welcome to ";
17-
COLOR 14
17+
COLOUR 14
1818
PRINT "Retro Rocket ";
19-
COLOR 7
19+
COLOUR 7
2020
PRINT "BASIC shell."
2121
PRINT MEMFREE; " bytes free."
2222
PRINT ""
@@ -34,9 +34,9 @@ END
3434

3535
REM Prompt display
3636
DEF PROCdisplayPrompt
37-
COLOR 14
37+
COLOUR 14
3838
PRINT PROMPT$;
39-
COLOR 7
39+
COLOUR 7
4040
PRINT "> ";
4141
ENDPROC
4242

@@ -131,9 +131,9 @@ REM display version information
131131
DEF PROCversion
132132
PRINT "┌─────────────────────────────────────────────────────┐"
133133
PRINT "│ ";
134-
COLOR 14
134+
COLOUR 14
135135
PRINT "rocketsh ";
136-
COLOR 7
136+
COLOUR 7
137137
PRINT "Retro Rocket Shell version "; VERSION$; " │"
138138
PRINT "├─────────────────────────────────────────────────────┤"
139139
PRINT "│ Licensed under the Apache 2.0 License. │"
@@ -180,9 +180,9 @@ DEF PROCbuiltinHistory
180180

181181
entry = 1
182182
FOR i = EDhistPtr - 1 TO 1 STEP -1
183-
COLOR 14
183+
COLOUR 14
184184
PRINT entry;
185-
COLOR 7
185+
COLOUR 7
186186
PRINT "> ";
187187
PRINT EDhist$(i)
188188
entry = entry + 1
@@ -308,9 +308,9 @@ DEF PROCbuiltinMultiLineRepl
308308
MULTI$ = ""
309309
multiLine = 1
310310
REPEAT
311-
COLOR 14
311+
COLOUR 14
312312
PRINT multiLine;
313-
COLOR 7
313+
COLOUR 7
314314
PRINT "> ";
315315
PROCEdLine
316316
PRINT ""

src/basic/function.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ struct basic_int_fn builtin_int[] =
179179
{ basic_tell, "TELL" },
180180
{ basic_memrealloc, "MEMREALLOC" },
181181
{ basic_string_to_buffer, "STRINGTOBUFFER" },
182+
{ basic_getproccpu, "GETPROCCPU" },
182183
{ NULL, NULL },
183184
};
184185

src/basic/process.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ int64_t basic_getprocid(struct basic_ctx* ctx)
4242
return proc_id(intval);
4343
}
4444

45+
int64_t basic_getproccpu(struct basic_ctx* ctx)
46+
{
47+
PARAMS_START;
48+
PARAMS_GET_ITEM(BIP_INT);
49+
PARAMS_END("GETPROCCPU", 0);
50+
return proc_cpu_percent(intval);
51+
}
52+
4553
char* basic_getprocname(struct basic_ctx* ctx)
4654
{
4755
PARAMS_START;

0 commit comments

Comments
 (0)