?? RIGHT := 110 ??
?? NEWTITLE := 'NOS/VE: system_line_console_display' ??
MODULE ofm$build_system_line;

{ Purpose:
{         The purpose of this module is to provide a single, consistant
{         block of code which displays the system_console line to the
{         operator console.  Currently, the system console line consists of
{         the idle_statistics for each cpu that is in use, and the percentage
{         of time spent in NOS-170 mode.

?? TITLE := '  Types, XREF Variables, and XREF Procedures', EJECT ??
?? PUSH (LISTEXT := ON) ??
*copyc oft$system_line_info
*copyc osc$multiprocessor_constants
*copyc ost$cpu_idle_statistics
?? POP ??
*copyc mtv$total_nos_cpu_time
*copyc mtv$cst0
*copyc osv$cpus_physically_configured
*copyc osv$170_os_type


?? TITLE := 'binary_to_2_ascii', EJECT ??
{
{ The following inline procedure converts an integer to a 2 character
{ string. If the integer is < 0, then ' 0' is returned. If the integer
{ is > 99 then '99' is returned.

  PROCEDURE [INLINE] binary_to_2_ascii
    (    xn: integer;
     VAR s: string (2));

    VAR
      n: 0 .. 99;

    IF xn < 0 THEN
      s := ' 0';
    ELSEIF xn > 99 THEN
      s := '99';
    ELSE
      n := xn;
      s (2) := $CHAR ((n MOD 10) + $INTEGER ('0'));
      n := n DIV 10;
      IF n > 0 THEN
        s (1) := $CHAR (n + $INTEGER ('0'));
      IFEND;
    IFEND;

  PROCEND binary_to_2_ascii;

?? TITLE := 'ofp$build_system_line', EJECT ??

  PROCEDURE [XDCL] ofp$build_system_line
    (VAR last_info: oft$system_line_info;
     VAR s: string ( * <= 250));

    VAR
      clocktime: integer,
      cpu_index: integer,
      cpu_io_utilization: integer,
      cpu_stats: ost$cpu_idle_statistics,
      cpu_utilization: integer,
      i: integer,
      idletime: integer,
      idle_string: string (7),
      initialized: boolean,
      nostime: integer,
      nos_util: integer;

    s (1, * ) := ' CPU Idle:';

    clocktime := #FREE_RUNNING_CLOCK (0);
    nostime := mtv$total_nos_cpu_time.total;
    initialized := last_info.initialized;

    FOR i := 0 TO osv$cpus_physically_configured - 1 DO
      cpu_stats := mtv$cst0 [i].cpu_idle_statistics;
      idletime := cpu_stats.idle_io_active + cpu_stats.idle_no_io_active;
      IF (mtv$cst0 [i].processor_state = cmc$on) AND initialized THEN
        IF cpu_stats.idle_count = last_info.idle_count [i] THEN
          IF cpu_stats.idle_type = osc$not_idle THEN {cpu utilitization is really the IDLE time}
            idle_string := '  0/ 0,';
          ELSEIF cpu_stats.idle_type = osc$idle_no_io_active THEN
            idle_string := ' 99/ 0,';
          ELSE
            idle_string := ' 99/99,';
          IFEND;
        ELSE
          idle_string := '   /  ,';
          cpu_io_utilization := (((cpu_stats.idle_io_active - last_info.last_io_idletime [i]) * 100) DIV
                (clocktime - last_info.last_clocktime));
          cpu_utilization := (((idletime - last_info.last_idletime [i]) * 100) DIV
                (clocktime - last_info.last_clocktime));
          binary_to_2_ascii (cpu_utilization, idle_string (2, 2));
          binary_to_2_ascii (cpu_io_utilization, idle_string (5, 2));
        IFEND;
        s (i * 7 + 11, 7) := idle_string;
        cpu_index := i;
      IFEND;
      last_info.idle_count [i] := cpu_stats.idle_count;
      last_info.last_idletime [i] := idletime;
      last_info.last_io_idletime [i] := cpu_stats.idle_io_active;
    FOREND;

    IF initialized THEN
      s (cpu_index * 7 + 17) := ' ';
      IF osv$170_os_type <> osc$ot7_none THEN
        nos_util := (((nostime - last_info.last_nostime) * 100) DIV
              (clocktime - last_info.last_clocktime)) MOD 512;
        IF osv$170_os_type = osc$ot7_dual_state_nos_be THEN
          s (69, 4) := 'NBE:';
        ELSE
          s (69, 4) := 'NOS:';
        IFEND;
        binary_to_2_ascii (nos_util, s (74, 2));
      IFEND;
    IFEND;


    last_info.last_nostime := nostime;
    last_info.last_clocktime := clocktime;
    last_info.initialized := TRUE;

  PROCEND ofp$build_system_line;

MODEND ofm$build_system_line
