Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

gkemu.c

Go to the documentation of this file.
00001 /*
00002  * gkemu.c
00003  *
00004  * Copyright (c) 2003 The University of Utah and the Flux Group.
00005  * All rights reserved.
00006  *
00007  * This file is licensed under the terms of the GNU Public License.  
00008  * See the file "license.terms" for restrictions on redistribution 
00009  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010  */
00011 
00012 /**
00013  * @file gkemu.c
00014  *
00015  * Implementation of the functions in gkemu.h.
00016  */
00017 
00018 #include "config.h"
00019 
00020 #include <time.h>
00021 #include <stdio.h>
00022 #include <stdarg.h>
00023 #include <string.h>
00024 
00025 #include <sys/time.h>
00026 
00027 #include <assert_pp.h>
00028 
00029 #include "gkemu.h"
00030 
00031 const char *gkInitialUpdateOpen = "<initial_update>\n";
00032 const char *gkInitialUpdateClose = "</initial_update>\n";
00033 
00034 int gkFormatPreamble(char *buf, size_t buf_len, gka_tag_t tag, ...)
00035 {
00036     struct {
00037         char *hostnametag, *hostname;
00038         char *sysnametag, *sysname;
00039     } preamble_args = {
00040         "", "",
00041         "", "",
00042     };
00043 
00044     time_t current_time;
00045     struct tm *tm;
00046     va_list args;
00047     int retval;
00048 
00049     require(buf != NULL);
00050     require(buf_len > 0);
00051 
00052     /* Gather the arguments from the tag list. */
00053     va_start(args, tag);
00054     while( tag != GKA_TAG_DONE )
00055     {
00056         switch( tag )
00057         {
00058         case GKA_HostName:
00059             preamble_args.hostname = va_arg(args, char *);
00060             if( preamble_args.hostname != NULL )
00061                 preamble_args.hostnametag = "<hostname>\n";
00062             break;
00063         case GKA_SystemName:
00064             preamble_args.sysname = va_arg(args, char *);
00065             if( preamble_args.sysname != NULL )
00066                 preamble_args.sysnametag = "<sysname>\n";
00067             break;
00068         default:
00069             ensure(0);
00070             break;
00071         }
00072         tag = va_arg(args, gka_tag_t);
00073     }
00074     va_end(args);
00075 
00076     /* Get the current time.  XXX Doesn't seem to work at the moment... */
00077     time(&current_time);
00078     tm = localtime(&current_time);
00079     /* Fill the buffer. */
00080     snprintf(buf,
00081              buf_len,
00082              "<gkrellmd_setup>\n"
00083              "<decimal_point>\n"
00084              ".\n"
00085              "\n"
00086              "<cpu_setup>\n"
00087              "n_cpus 2\n"
00088              "%s"
00089              "%s\n"
00090              "%s"
00091              "%s\n"
00092              "<time>\n"
00093              "%d %d %d %d %d %d %d %d %d\n"
00094              "<monitors>\n"
00095              "cpu\n"
00096              "proc\n"
00097              "uptime\n"
00098              "</gkrellmd_setup>\n",
00099              preamble_args.hostnametag,
00100              preamble_args.hostname,
00101              preamble_args.sysnametag,
00102              preamble_args.sysname,
00103              tm->tm_sec, tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon,
00104              tm->tm_year, tm->tm_wday, tm->tm_yday, tm->tm_isdst);
00105     retval = strlen(buf);
00106     return( retval );
00107 }
00108 
00109 int gkFormatUpdate(char *buf, size_t buf_len, gka_tag_t tag, ...)
00110 {
00111     static struct tm last_tm;
00112     
00113     struct {
00114         unsigned long long cpu_user, cpu_idle;
00115         unsigned long long rsv_user, rsv_nice, rsv_idle;
00116         unsigned long processes, processes_running;
00117         unsigned long uptime;
00118     } update_args = {
00119         0, 0,
00120         0, 0, 0,
00121         0, 0,
00122     };
00123     time_t current_time;
00124     struct tm *tm;
00125     va_list args;
00126     int retval;
00127 
00128     require(buf != NULL);
00129     require(buf_len > 0);
00130     
00131     va_start(args, tag);
00132     while( tag != GKA_TAG_DONE )
00133     {
00134         switch( tag )
00135         {
00136         case GKA_CPUUser:
00137             update_args.cpu_user = va_arg(args, unsigned long long);
00138             break;
00139         case GKA_CPUIdle:
00140             update_args.cpu_idle = va_arg(args, unsigned long long);
00141             break;
00142         case GKA_CPUReserveUser:
00143             update_args.rsv_user = va_arg(args, unsigned long long);
00144             break;
00145         case GKA_CPUReserveNice:
00146             update_args.rsv_nice = va_arg(args, unsigned long long);
00147             break;
00148         case GKA_CPUReserveIdle:
00149             update_args.rsv_idle = va_arg(args, unsigned long long);
00150             break;
00151         case GKA_Processes:
00152             update_args.processes = va_arg(args, unsigned int);
00153             break;
00154         case GKA_ProcessesRunning:
00155             update_args.processes_running = va_arg(args, unsigned int);
00156             break;
00157         case GKA_UpTime:
00158             {
00159                 struct timeval *tv = va_arg(args, struct timeval *);
00160 
00161                 /* GKrellm expects uptime in minutes. */
00162                 update_args.uptime = tv->tv_sec / 60;
00163             }
00164             break;
00165         default:
00166             ensure(0);
00167             break;
00168         }
00169         tag = va_arg(args, gka_tag_t);
00170     }
00171     va_end(args);
00172     
00173     time(&current_time);
00174     tm = localtime(&current_time);
00175     snprintf(buf,
00176              buf_len,
00177              "<cpu>\n"
00178              "0 %qd 0 0 %qd\n"
00179              "1 %qd %qd 0 %qd\n"
00180              "<proc>\n"
00181              "%ld %ld 0 0.0 1\n"
00182              "<uptime>\n"
00183              "%ld\n",
00184              update_args.cpu_user, update_args.cpu_idle,
00185              update_args.rsv_user, update_args.rsv_nice, update_args.rsv_idle,
00186              update_args.processes, update_args.processes_running,
00187              update_args.uptime);
00188     retval = strlen(buf);
00189     if( last_tm.tm_min != tm->tm_min )
00190     {
00191         snprintf(&buf[retval],
00192                  buf_len - retval,
00193                  "<time>\n"
00194                  "%d %d %d %d %d %d %d %d %d\n",
00195                  tm->tm_sec, tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon,
00196                  tm->tm_year, tm->tm_wday, tm->tm_yday, tm->tm_isdst);
00197     }
00198     else
00199     {
00200         snprintf(&buf[retval],
00201                  buf_len - retval,
00202                  "<.%d>\n",
00203                  tm->tm_sec);
00204     }
00205     retval = strlen(buf);
00206     last_tm = *tm;
00207     return( retval );
00208 }

Generated on Mon Dec 1 16:29:06 2003 for CPU Broker by doxygen 1.3.4