home *** CD-ROM | disk | FTP | other *** search
/ The AGA Experience 2 / agavol2.iso / software / utilities / wb_tools / executive_v1.30 / sysinfo.lzx / examples / test / test.c < prev   
C/C++ Source or Header  |  2008-01-11  |  8KB  |  274 lines

  1. /*
  2.  * Test-program for some SysInfo.library features
  3.  *
  4.  * This file is public domain.
  5.  *
  6.  * Author: Petri Nordlund <petrin@megabaud.fi>
  7.  *
  8.  * $Id: test.c 1.3 1995/10/12 16:31:32 petrin Exp petrin $
  9.  *
  10.  */
  11.  
  12. #include "defs.h"
  13. #include <proto/SysInfo.h>
  14. #include <libraries/SysInfo.h>
  15.  
  16. /* VARIABLES */
  17. struct SysInfo *si = NULL;
  18. struct Library *SysInfoBase = NULL;
  19.  
  20.  
  21. int
  22. main(int argc, char **argv)
  23. {
  24.     if(!(SysInfoBase = OpenLibrary(SYSINFONAME, SYSINFOVERSION)))
  25.     {
  26.         puts("Can't open SysInfo.library");
  27.         exit(RETURN_FAIL);
  28.     }
  29.  
  30.  
  31.     /* Initialize the SysInfo.library, this will make connection to the
  32.      * server-process and allocate the SysInfo-structure. */
  33.     if(!(si = InitSysInfo()))
  34.     {
  35.         puts("Couldn't initialize SysInfo");
  36.         CloseLibrary(SysInfoBase);
  37.         exit(RETURN_FAIL);
  38.     }
  39.  
  40.  
  41.     /* print our pid, ppid and pgrp */
  42.     printf("pid:  %d\n",GetPid(si));
  43.     if(si->GetPpid_implemented)
  44.     {
  45.         LONG p = GetPpid(si);
  46.         if(p != -1)
  47.             printf("ppid: %d\n",p);
  48.         else
  49.             printf("ppid: unknown\n");
  50.     }
  51.     if(si->GetPgrp_implemented)
  52.     {
  53.         LONG p = GetPgrp(si);
  54.         if(p != -1)
  55.             printf("pgrp: %d\n",p);
  56.         else
  57.             printf("pgrp: unknown\n");
  58.     }
  59.  
  60.  
  61.     /* If si->which_implemented is 0, then GetNice() and SetNice() are not available */
  62.     /* We'll also make sure that the search methods we need have been implemented    */
  63.     if(si->which_implemented && (si->which_implemented & (WHICHF_PRIO_TASK | WHICH_PRIO_PROCESS)))
  64.     {
  65.         LONG nice;
  66.  
  67.         /* display the nice-value for this task */
  68.         nice=GetNice(si,WHICH_PRIO_TASK,0);
  69.         if(nice == -1)
  70.         {
  71.             if(si->errno)
  72.                 printf("GetNice() failed, errno: %d\n",si->errno);
  73.             else
  74.                 printf("nice: %d\n",nice);
  75.         }
  76.         else
  77.             printf("nice: %d\n",nice);
  78.  
  79.         /* set our nice-value to +5 */
  80.         if(SetNice(si,WHICH_PRIO_PROCESS,GetPid(si),5))
  81.             printf("SetNice() failed, errno: %d\n",si->errno);
  82.     }
  83.  
  84.  
  85.     /* Ask for notify and output load averages every second for 10 seconds. */
  86.  
  87.     if(si->loadavg_type != LOADAVG_NONE)
  88.     {
  89.         struct SI_Notify *not;
  90.         struct Message *msg;
  91.         short i;
  92.  
  93.         if(si->notify_msg_implemented && (not=AddNotify(si,AN_USE_MESSAGES,10)))
  94.         {
  95.             printf("load averages (%d.%02d, %d.%02d, %d.%02d minutes):\n",
  96.                 si->loadavg_time1/60, si->loadavg_time1%60,
  97.                 si->loadavg_time2/60, si->loadavg_time2%60,
  98.                 si->loadavg_time3/60, si->loadavg_time3%60);
  99.  
  100.             for(i=0;i<10;i++)
  101.             {
  102.                 /* We'll get a message every second. There may be more than
  103.                  * one message in the port at once. */
  104.  
  105.                 Wait(1L<<not->notify_port->mp_SigBit);
  106.                 while(msg = GetMsg(not->notify_port))
  107.                 {
  108.                     struct SI_LoadAverage load;    /* This will be filled by GetLoadAverage() */
  109.  
  110.                     ReplyMsg(msg);
  111.  
  112.                     GetLoadAverage(si, &load);    /* Ask SysInfo.library for current load averages */
  113.  
  114.                     printf("load average:");
  115.  
  116.                     switch(si->loadavg_type)
  117.                     {
  118.                         case LOADAVG_FIXEDPNT:
  119.  
  120.                             /* Convert fixed point values to floating point values */
  121.  
  122.                             if(si->loadavg_time1)
  123.                                 printf(" %.2f",(float) load.lavg_fixed.load1 / (float) si->fscale);
  124.                             else
  125.                                 printf(" N/A");
  126.  
  127.                             if(si->loadavg_time2)
  128.                                 printf(" %.2f",(float) load.lavg_fixed.load2 / (float) si->fscale);
  129.                             else
  130.                                 printf(" N/A");
  131.  
  132.                             if(si->loadavg_time3)
  133.                                 printf(" %.2f\n",(float) load.lavg_fixed.load3 / (float) si->fscale);
  134.                             else
  135.                                 printf(" N/A\n");
  136.  
  137.                             break;
  138.                     }
  139.                 }
  140.             }
  141.             RemoveNotify(si,not);
  142.         }
  143.         else
  144.             printf("Can't use notification.\n");
  145.     }
  146.     else
  147.         printf("Load averages are not supported.\n");
  148.  
  149.  
  150.     /* output cpu usage values */
  151.     {
  152.         struct SI_CpuUsage cu;
  153.  
  154.         GetCpuUsage(si,&cu);
  155.  
  156.         printf("cpu time:                 ");
  157.         if(si->cpu_usage_implemented & CPU_USAGEF_TOTAL_IMPLEMENTED)
  158.             printf("%d seconds used, %d seconds idle\n",cu.total_used_cputime, cu.total_elapsed_time - cu.total_used_cputime);
  159.         else
  160.             printf("N/A\n");
  161.  
  162.         printf("cpu usage:                ");
  163.         if(si->cpu_usage_implemented & CPU_USAGEF_TOTAL_IMPLEMENTED)
  164.             printf("%.2f%%\n",((float) cu.total_used_cputime * 100.0) / (float) cu.total_elapsed_time);
  165.         else
  166.             printf("N/A\n");
  167.  
  168.         printf("current cpu usage:        ");
  169.         if(si->cpu_usage_implemented & CPU_USAGEF_LASTSEC_IMPLEMENTED)
  170.             printf("%.2f%%\n",((float) cu.used_cputime_lastsec * 100.0) / (float) cu.used_cputime_lastsec_hz);
  171.         else
  172.             printf("N/A\n");
  173.  
  174.         printf("recent cpu usage:         ");
  175.         if(si->cpu_usage_implemented & CPU_USAGEF_RECENT_IMPLEMENTED)
  176.             printf("%.2f%% (%d seconds)\n",((float) cu.recent_used_cputime * 100.0) / (float) cu.recent_used_cputime_hz, cu.recent_seconds);
  177.         else
  178.             printf("N/A\n");
  179.  
  180.         printf("context switches:         ");
  181.         if(si->cpu_usage_implemented & CPU_USAGEF_IVVOCSW_IMPLEMENTED)
  182.             printf("%d involuntary, %d voluntary\n", cu.involuntary_csw, cu.voluntary_csw);
  183.         else
  184.             printf("N/A\n");
  185.  
  186.         printf("total context switches:   ");
  187.         if(si->cpu_usage_implemented & CPU_USAGEF_TOTALCSW_IMPLEMENTED)
  188.             printf("%d\n", cu.total_csw);
  189.         else
  190.             printf("N/A\n");
  191.  
  192.         printf("current context switches: ");
  193.         if(si->cpu_usage_implemented & CPU_USAGEF_IVVOCSW_LASTSEC_IMPLEMENTED)
  194.             printf("%d involuntary, %d voluntary\n", cu.involuntary_csw_lastsec, cu.voluntary_csw_lastsec);
  195.         else
  196.             printf("N/A\n");
  197.  
  198.         printf("current total csws:       ");
  199.         if(si->cpu_usage_implemented & CPU_USAGEF_TOTALCSW_LASTSEC_IMPLEMENTED)
  200.             printf("%d\n", cu.total_csw_lastsec);
  201.         else
  202.             printf("N/A\n");
  203.     }
  204.  
  205.  
  206.     /* output cpu usage values for this task */
  207.     {
  208.         struct SI_TaskCpuUsage cu;
  209.  
  210.         if(!GetTaskCpuUsage(si,&cu,0))
  211.         {
  212.             printf("This task:\n");
  213.  
  214.             printf("cpu time:                   ");
  215.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTAL_IMPLEMENTED)
  216.                 printf("%d.%d seconds\n",cu.total_used_cputime / cu.total_used_time_hz, cu.total_used_cputime % cu.total_used_time_hz);
  217.             else
  218.                 printf("N/A\n");
  219.  
  220.             printf("cpu usage:                  ");
  221.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTAL_IMPLEMENTED)
  222.                 printf("%.2f%%\n",(((float) cu.total_used_cputime) / ((float) cu.total_used_time_hz) * 100.0) / (float) cu.total_elapsed_time);
  223.             else
  224.                 printf("N/A\n");
  225.  
  226.             printf("current cpu usage:          ");
  227.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_LASTSEC_IMPLEMENTED)
  228.                 printf("%.2f%%\n",((float) cu.used_cputime_lastsec * 100.0) / (float) cu.used_cputime_lastsec_hz);
  229.             else
  230.                 printf("N/A\n");
  231.  
  232.             printf("recent cpu usage:           ");
  233.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_RECENT_IMPLEMENTED)
  234.                 printf("%.2f%% (%d seconds)\n",((float) cu.recent_used_cputime * 100.0) / (float) cu.recent_used_cputime_hz, cu.recent_seconds);
  235.             else
  236.                 printf("N/A\n");
  237.  
  238.             printf("context switches:           ");
  239.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_IVVOCSW_IMPLEMENTED)
  240.                 printf("%d involuntary, %d voluntary\n", cu.involuntary_csw, cu.voluntary_csw);
  241.             else
  242.                 printf("N/A\n");
  243.  
  244.             printf("total context switches:     ");
  245.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTALCSW_IMPLEMENTED)
  246.                 printf("%d\n", cu.total_csw);
  247.             else
  248.                 printf("N/A\n");
  249.  
  250.             printf("context switches (ps):      ");
  251.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_IVVOCSW_LASTSEC_IMPLEMENTED)
  252.                 printf("%d involuntary, %d voluntary\n", cu.involuntary_csw_lastsec, cu.voluntary_csw_lastsec);
  253.             else
  254.                 printf("N/A\n");
  255.  
  256.             printf("total context switches (ps):");
  257.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTALCSW_LASTSEC_IMPLEMENTED)
  258.                 printf("%d\n", cu.total_csw_lastsec);
  259.             else
  260.                 printf("N/A\n");
  261.         }
  262.         else
  263.             printf("Can't get CPU usage for this task.\n");
  264.     }
  265.  
  266.     if(si)
  267.         FreeSysInfo(si);
  268.  
  269.     if(SysInfoBase)
  270.         CloseLibrary(SysInfoBase);
  271.  
  272.     return(RETURN_OK);
  273. }
  274.