00001 /* 00002 * childProcess.h 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 childProcess.h 00014 * 00015 * Header file for the child process accounting functions. 00016 */ 00017 00018 #ifndef _child_process_h 00019 #define _child_process_h 00020 00021 #ifdef __cplusplus 00022 extern "C" { 00023 #endif 00024 00025 #include <sys/types.h> 00026 00027 #include <listNode.h> 00028 00029 /* 00030 * A cpChildProcess is used to track and record the resource usage of a child 00031 * process. 00032 * 00033 * cp_Link - List link. 00034 * cp_PID - The process ID of the child. 00035 * cp_ProcFileName - The path to the process stats for this child in /proc. 00036 * cp_Output - The statistics file for this child. 00037 * cp_LastUsage - The last recorded usage for this child. Used to compute 00038 * the difference in usage between the current and last sample time. 00039 */ 00040 struct cpChildProcess { 00041 struct lnMinNode cp_Link; 00042 pid_t cp_PID; 00043 char *cp_ProcFileName; 00044 FILE *cp_Output; 00045 #if defined(linux) || defined(__FreeBSD__) 00046 struct timeval cp_LastUsage; 00047 #else 00048 #error "Implement me" 00049 #endif 00050 }; 00051 00052 enum { 00053 CPDB_INITIALIZED, 00054 }; 00055 00056 /* 00057 * Flags for the cpChildProcessData structure. 00058 * 00059 * CPDF_INITIALIZED - The global data is initialized. 00060 */ 00061 enum { 00062 CPDF_INITIALIZED = (1L << CPDB_INITIALIZED), 00063 }; 00064 00065 /* 00066 * Global data for tracking child processes. 00067 * 00068 * cpd_Flags - Holds the CPDF_ flags. 00069 * cpd_Children - The list of child processes. 00070 */ 00071 struct cpChildProcessData { 00072 unsigned long cpd_Flags; 00073 struct lnMinList cpd_Children; 00074 }; 00075 00076 /** 00077 * Initialize the internal accounting data structures. 00078 * 00079 * @return True on success, false otherwise. 00080 */ 00081 int cpInitChildProcessData(void); 00082 00083 /** 00084 * Deinitialize the internal accounting data structures. 00085 */ 00086 void cpKillChildProcessData(void); 00087 00088 /** 00089 * Find the cpChildProcess structure that corresponds to the given ID. 00090 * 00091 * @param child_pid The child process to track. 00092 * @return The cpChildProcess object that corresponds to the given ID or NULL 00093 * if one has not been created yet. 00094 */ 00095 struct cpChildProcess *cpFindChildProcess(pid_t child_pid); 00096 00097 /** 00098 * Create a cpChildProcess object for a child that does not have one yet. 00099 * 00100 * @param child_pid The child process to track. 00101 * @return The newly creaetd object or NULL if a problem was encountered 00102 * during creation. 00103 */ 00104 struct cpChildProcess *cpCreateChildProcess(pid_t child_pid); 00105 00106 /** 00107 * Delete the given cpChildProcess object. 00108 * 00109 * @param cp NULL or a previously created cpChildProcess object. 00110 */ 00111 void cpDeleteChildProcess(struct cpChildProcess *cp); 00112 00113 /** 00114 * Open and initialize the statistics output file for the given cpChildProcess 00115 * object. The created file will have the form "<base_name>-<pid>.out". 00116 * 00117 * @param cp A valid cpChildProcess object. 00118 * @param base_name The file name prefix. 00119 * @param start_time The start time for the parent process. 00120 * @return True if the file was created successfully, false otherwise. 00121 */ 00122 int cpOpenOutput(struct cpChildProcess *cp, 00123 char *base_name, 00124 struct timeval *start_time); 00125 00126 /** 00127 * Sample the resource usage for a given child process. 00128 * 00129 * @param cp A valid cpChildProcess object. 00130 * @param run_time The amount of time the parent process has been running. 00131 * @return The CPU usage, in microseconds, for the given child process. 00132 */ 00133 unsigned long long cpSampleUsage(struct cpChildProcess *cp, 00134 struct timeval *run_time); 00135 00136 #ifdef __cplusplus 00137 } 00138 #endif 00139 00140 #endif