GridLab
Grid Application Toolkit

A simple API for Grid Applications
GAT

Menu



Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

GATMetric.c

Go to the documentation of this file.
00001 /** @file GATMetric.c
00002  *  Source file for the GATMetric class.
00003  *
00004  *  @date Tue Nov 4 2003
00005  *
00006  *  @version $Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATMetric.c,v 1.13 2004/04/02 12:31:58 hartmutkaiser Exp $
00007  *
00008  *  Copyright (C) Hartmut Kaiser
00009  *  This file is part of the GAT Engine.
00010  *  Contributed by Hartmut Kaiser <hartmutkaiser [at] t-online [dot] de>.
00011  *
00012  *  Use, modification and distribution is subject to the Gridlab Software
00013  *  License. (See accompanying file GLlicense.txt or copy at
00014  *  http://www.gridlab.org/GLlicense.txt)
00015  */
00016  
00017 static const char *rcsid = "$Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATMetric.c,v 1.13 2004/04/02 12:31:58 hartmutkaiser Exp $";
00018  
00019 /* System Header objects */
00020 #include <stdlib.h>
00021 #include <string.h>
00022 
00023 /* GAT Header objects */
00024 #include "GAT.h"
00025 #include "GATInternal.h"
00026 #include "GATUtil.h"
00027 
00028 /* define the vtable types */
00029 GATOBJECT_DEFINE_VTABLE(GATMetric);
00030 
00031 /* define the converters to/from GATObject */
00032 GATOBJECT_DEFINE_CONVERTERS(GATMetric)
00033 
00034 /* Macros */
00035 
00036 /* Structures, unions and enums */
00037 struct GATMetric_S {
00038   /* supported interfaces */
00039   GATMetric_vtable *GATObject__vtable;
00040 
00041   /* instance data */
00042   char const *name;                     /* metric name */
00043   GATTable parameters;                  /* metric parameters */
00044   GATMeasurementType measurement_type;  /* metric measurement type */
00045   GATType data_type;                    /* metric data type */
00046   char const *unit;                     /* metric unit */
00047 };
00048 
00049 /* Static function prototypes */
00050 
00051 /* object scope variables */
00052 GATMetric_vtable GATMetric__vtable = {
00053   GATMetric_GetType,
00054   GATMetric_Destroy,
00055   GATMetric_Equals,
00056   GATMetric_Clone,
00057   GATMetric_GetInterface,
00058   NULL
00059 };
00060 
00061 /* External functions */
00062 
00063 /** GATMetric_Create
00064  *  @brief Create a new GATMetric object
00065  *
00066  *  The function @c GATMetric_Create creates a new GATMetric object.
00067  *
00068  *  @return Returns a handle to the newly created GATMetric object.
00069  *        Returns 0 (zero) if an error occurs.
00070  */
00071 GATMetric 
00072 GATMetric_Create(char const *name, GATTable parameters, 
00073   GATMeasurementType type, GATType datatype, char const *unit)
00074 {
00075   GATResult err_code = GAT_MEMORYFAILURE;
00076   GATMetric retval = (GATMetric) malloc(sizeof(struct GATMetric_S));
00077       
00078   if (NULL != retval)
00079   {
00080     memset(retval, 0, sizeof(struct GATMetric_S));
00081     retval->GATObject__vtable = &GATMetric__vtable;
00082     retval->measurement_type = type;
00083     retval->data_type = datatype;
00084     
00085     err_code = GATTable_Clone(parameters, &retval->parameters);
00086     if (GAT_SUCCESS == err_code)
00087     {
00088       retval->name = GATUtil_strdup(name);
00089       retval->unit = GATUtil_strdup(unit);
00090       if (NULL == retval->name || NULL == retval->unit)
00091       {
00092         err_code = GAT_MEMORYFAILURE;
00093       }
00094     }
00095   }
00096   
00097   /* error handling */
00098   if (GAT_SUCCESS != err_code)
00099   {
00100     /* FIXME: GATStatus(err_code) */
00101     if (NULL != retval)
00102     {
00103       GATMetric_Destroy(&retval);
00104     }
00105   }
00106   return retval;
00107 }
00108 
00109 /** void GATMetric_Destroy(GATMetric *resource)
00110  *
00111  *  The function GATMetric_Destroy is the destructor used to
00112  *  free all the memory allocated by an GATMetric instance.
00113  *
00114  *  @param description The pointer to the GATMetric to destroy
00115  */
00116 void
00117 GATMetric_Destroy(GATMetric *object)
00118 {
00119   if (NULL != object && NULL != *object)
00120   {
00121     GATTable_Destroy(&(*object)->parameters);
00122     free((char *) (*object)->name);
00123     free((char *) (*object)->unit);
00124     free(*object);
00125     *object = NULL;
00126   }
00127 }
00128 
00129 /** GATMetric_Equals
00130  *  @brief Compare two GATMetric objects
00131  *
00132  *  The function @c GATMetric_Equals compares two objects of the
00133  *  @c GATMetric type.
00134  *
00135  *  @param lhs The first list to compare
00136  *  @param rhs The second list to compare
00137  *  @param isequal The pointer to the location, where the outcome of the 
00138  *        function has to be stored.
00139  *
00140  *  @return An error code.
00141  */
00142 GATResult
00143 GATMetric_Equals(GATMetric_const lhs,
00144   GATMetric_const rhs, GATBool *isequal)
00145 {
00146   GATResult retval = GAT_INVALID_HANDLE;
00147 
00148   if (NULL != lhs && NULL != rhs)
00149   {
00150     if (NULL != isequal)
00151     {
00152       *isequal = GATFalse;
00153       retval = GAT_SUCCESS;
00154       
00155       if (!strcmp(lhs->name, rhs->name))
00156       {
00157         retval = GATTable_Equals(lhs->parameters, rhs->parameters, isequal);
00158       }
00159     }
00160     else
00161     {
00162       retval = GAT_INVALID_PARAMETER;
00163     }
00164   }
00165   return retval;
00166 }
00167 
00168 /** GATMetric_Clone
00169  *  @brief Clone the given GATMetric
00170  *
00171  *  The function @c GATMetric_Clone generates a (deep) copy of 
00172  *  the given GATMetric. 
00173  *
00174  *  @param description The object to clone
00175  *  @param new_object The pointer, through which the result is to be 
00176  *        returned.
00177  *
00178  *  @return An error type.
00179  */
00180 GATResult
00181 GATMetric_Clone(GATMetric_const handle, 
00182   GATMetric *new_handle)
00183 {
00184   GATResult retval = GAT_INVALID_HANDLE;
00185   if (NULL != handle)
00186   {
00187     if (NULL == new_handle)
00188     {
00189       retval = GAT_INVALID_PARAMETER;
00190     }
00191     else
00192     {
00193       GATMetric new_object = (GATMetric) malloc(sizeof(struct GATMetric_S));
00194       
00195 
00196       *new_handle = NULL;
00197       if (NULL == new_object)
00198       {
00199         retval = GAT_MEMORYFAILURE;
00200       }
00201       else
00202       {
00203         memset(new_object, 0, sizeof(struct GATMetric_S));
00204         new_object->GATObject__vtable = &GATMetric__vtable;
00205         new_object->measurement_type = handle->measurement_type;
00206         new_object->data_type = handle->data_type;
00207         
00208         retval = GATTable_Clone(handle->parameters, &new_object->parameters);
00209         if (GAT_SUCCESS == retval)
00210         {
00211           new_object->name = GATUtil_strdup(handle->name);
00212           new_object->unit = GATUtil_strdup(handle->unit);
00213           if (NULL == new_object->name || NULL == new_object->unit)
00214           {
00215             retval = GAT_MEMORYFAILURE;
00216           }
00217         }
00218         
00219         if (GAT_SUCCESS == retval)
00220         {
00221           *new_handle = new_object;
00222         }
00223         else        
00224         {
00225           GATMetric_Destroy(&new_object);
00226         }
00227       }
00228     }
00229   }
00230   return retval;
00231 }
00232 
00233 /** GATType GATMetric_GetType(GATMetric_const resource)
00234  *  @brief Return the type of the GATMetric
00235  *
00236  *  The function @c GATMetric_GetType always returns 
00237  *  @c #GATType_GATMetric. 
00238  *
00239  *  @param object The object to inspect
00240  *
00241  *  @return Returns always @c #GATType_GATMetric. 
00242  */
00243 GATType
00244 GATMetric_GetType(GATMetric_const description)
00245 {
00246   GAT_UNUSED_PARAMETER(description);
00247   return GATType_GATMetric;
00248 }
00249 
00250 /** int GATMetric_GetInterface(GATMetric_const file, GATInterface iftype, void const **ifp)
00251  *  @brief Get an interface supported by a GATObject
00252  *
00253  *  The function GATMetric_GetInterface allows to get a pointer to an 
00254  *  additional interface supported by this GATMetric.
00255  *
00256  *  @param object The object to be asked for the new interface.
00257  *  @param iftype The interface the object is to be asked for.
00258  *  @param ifp The pointer, through which the result is to be returned.
00259  *
00260  *  @return An error type.
00261  */
00262 GATResult 
00263 GATMetric_GetInterface(GATMetric_const metric, GATInterface iftype, 
00264   void const **ifp)
00265 {
00266   GATResult retval = GAT_INVALID_PARAMETER;
00267   
00268   GAT_UNUSED_PARAMETER(metric);
00269   GAT_UNUSED_PARAMETER(iftype);
00270 
00271   if (NULL != ifp)
00272   {
00273     *ifp = NULL;
00274     retval = GAT_NO_INTERFACE;
00275   }
00276   return retval;
00277 }
00278 
00279 /** GATMetric_i_GetTypeSize
00280  *
00281  *  @return The size of the internal representation of the GATMetric type.
00282  *
00283  *  @remark This is an internal function, do not use it directly.
00284  */
00285 GATuint32 GATMetric_i_GetTypeSize(void)
00286 { 
00287   return sizeof(struct GATMetric_S);
00288 }  
00289 
00290 /* GATMetric API functions */
00291 
00292 /** GATMetric_GetName
00293  *
00294  *  The function GATMetric_GetName gets the name associated with this 
00295  *  GATMetric.
00296  *
00297  *  @param metric The metric to ask for its name.
00298  *
00299  *  @return The name associated with this metric.
00300  */
00301 char const *
00302 GATMetric_GetName(GATMetric_const metric)
00303 {
00304   char const *name = NULL;
00305   if (NULL != metric)
00306   {
00307     name = metric->name;
00308   }
00309   return name;
00310 }
00311 
00312 /** GATMetric_GetUnit
00313  *
00314  *  The function GATMetric_GetUnit gets the measurement unit 
00315  *  associated with this GATMetric.
00316  *
00317  *  @param metric The metric to ask for its measurement unit.
00318  *
00319  *  @return The unit associated with this metric.
00320  */
00321 char const *
00322 GATMetric_GetUnit(GATMetric_const metric)
00323 {
00324   char const *unit = NULL;
00325   if (NULL != metric)
00326   {
00327     unit = metric->unit;
00328   }
00329   return unit;
00330 }
00331 
00332 /** GATMetric_GetValueType
00333  *
00334  *  The function GATMetric_GetValueType gets the data type of the value 
00335  *  provided by this GATMetric.
00336  *
00337  *  @param metric The metric to ask for its data value type.
00338  *
00339  *  @return The data value type associated with this metric.
00340  */
00341 GATType
00342 GATMetric_GetValueType(GATMetric_const metric)
00343 {
00344   GATType type = GATType_NoType;
00345   if (NULL != metric)
00346   {
00347     type = metric->data_type;
00348   }
00349   return type;
00350 }  
00351 
00352 /** GATMetric_GetParameters
00353  *
00354  *  The function GATMetric_GetParameters gets the GATMetric parameter 
00355  *  value associated with the passed GATMetric parameter name. Ther is returned 
00356  *  a 0 (zero) if there is no GATMetric parameter value with the passed name.
00357  *
00358  *  @param metric The metric to ask for its name.
00359  *  @param table The pointer to a variable, which receives the returned 
00360  *        parameter table.
00361  *
00362  *  @return An error code.
00363  */
00364 GATResult 
00365 GATMetric_GetParameters(GATMetric_const metric, GATTable *table)
00366 {
00367   GATResult retval = GAT_INVALID_HANDLE;
00368   if (NULL != metric)
00369   {
00370     retval = GATTable_Clone(metric->parameters, table);
00371   }
00372   return retval;
00373 }
00374 
00375 /** GATMetric_GetParameterTypeByName
00376  *
00377  *  The function GATMetric_GetParameterTypeByName gets the type of the 
00378  *  GATMetric parameter value associated with the passed GATMetric parameter 
00379  *  name. 
00380  *
00381  *  @param metric The metric to ask for its name.
00382  *  @param name The name of the parameter to query for.
00383  *
00384  *  @return The type of the associated value.
00385  */
00386 GATType
00387   GATMetric_GetParameterTypeByName(GATMetric_const metric, 
00388     char const *name)
00389 {
00390   GATType type = GATType_NoType;
00391   if (NULL != metric)
00392   {
00393     type = GATTable_Get_ElementType(metric->parameters, name);
00394   }
00395   return type;
00396 }
00397 
00398 /** GATMetric_GetParameterByName
00399  *
00400  *  The function GATMetric_GetParameterByName gets the GATMetric 
00401  *  parameter value associated with the passed GATMetric parameter name. There 
00402  *  is returned a 0 (zero) if there is no GATMetric parameter value with the 
00403  *  passed name.
00404  *
00405  *  @param metric The metric to ask for its name.
00406  *  @param name The name of the parameter to query for.
00407  *  @param buffer The buffer, where the retrieved value should be returned to.
00408  *  @param size The size of the buffer.
00409  *
00410  *  @return An error code.
00411  */
00412 GATResult
00413 GATMetric_GetParameterByName(GATMetric_const metric, char const *name,
00414   GATType type, void *buffer, GATuint32 size)
00415 {
00416   GATResult retval = GAT_INVALID_HANDLE;
00417   if (NULL != metric)
00418   {
00419     switch(type)
00420     {
00421       case GATType_GATint16:
00422         retval = GATTable_Get_short(metric->parameters, name, 
00423           (GATint16 *) buffer);
00424         break;
00425         
00426       case GATType_GATint32:
00427         retval = GATTable_Get_int(metric->parameters, name, 
00428           (GATint32 *) buffer);
00429         break;
00430         
00431       case GATType_GATfloat32:
00432         retval = GATTable_Get_float(metric->parameters, name, 
00433           (GATfloat32 *) buffer);
00434         break;
00435         
00436       case GATType_GATdouble64:
00437         retval = GATTable_Get_double(metric->parameters, name, 
00438           (GATdouble64 *) buffer);
00439         break;
00440         
00441       case GATType_String:
00442         retval = GATTable_Get_String(metric->parameters, name, 
00443           (char *) buffer, size);
00444         break;
00445         
00446       case GATType_GATObject:
00447         retval = GATTable_Get_GATObject(metric->parameters, name, 
00448           (GATObject_const *) buffer);
00449         break;
00450 
00451       default:
00452         retval = GAT_KEY_TYPE_ERROR;
00453         break;
00454     }
00455   }
00456   return retval;
00457 }
00458 
00459 /** GATMetric_GetMeasurementType
00460  *
00461  *  The functions GATMetric_GetMeasurementType returns the measurment type of
00462  *  the value associated with the given metric.
00463  *
00464  *  @param metric The metric to ask for its values measurement type.
00465  *
00466  *  @return The requested measurement type.
00467  */
00468 GATMeasurementType GATMetric_GetMeasurementType(GATMetric_const metric)
00469 {
00470   GATMeasurementType retval = GATMeasurementType_Unknown;
00471   if (NULL != metric)
00472   {
00473     retval = metric->measurement_type;
00474   }
00475   return retval;
00476 }
00477 
00478 /** GATMetric_CreateMetric
00479  *
00480  *  The function GATMetric_CreateMetric creates a GATMetric object from a given 
00481  *  static metric data. This is a pure helper function for adaptor writers.
00482  *
00483  *  @param data The address of the static metric data.
00484  *  @param metric The pointer to the variable, which receives the resulting 
00485  *        metric.
00486  *
00487  *  @return An error code.
00488  */
00489 GATResult
00490 GATMetric_CreateMetric(GATStaticMetric const *data, GATMetric *metric)
00491 {
00492   GATResult retval = GAT_INVALID_PARAMETER;
00493   if (NULL != data && NULL != metric)
00494   {
00495     /* create an fill the parameters table */
00496     GATTable parameters = GATTable_Create();
00497     
00498     *metric = NULL;
00499     if (NULL == parameters)
00500     {
00501       retval = GAT_MEMORYFAILURE;
00502     }
00503     else 
00504     {
00505 
00506       GATuint32 j = 0;
00507       GATStaticParameter *p = data->parameters;
00508       
00509       retval = GAT_SUCCESS;     /* no parameters is ok too */
00510       for (/**/; j < data->parameter_count; ++j, ++p)
00511       {
00512         switch(p->type)
00513         {
00514           case GATType_GATint16:
00515             retval = GATTable_Add_short(parameters, p->key, p->d.int16_data);
00516             break;
00517             
00518           case GATType_GATint32:
00519             retval = GATTable_Add_int(parameters, p->key, p->d.int32_data);
00520             break;
00521             
00522           case GATType_GATfloat32:
00523             retval = GATTable_Add_float(parameters, p->key, p->d.float_data);
00524             break;
00525             
00526           case GATType_GATdouble64:
00527             retval = GATTable_Add_double(parameters, p->key, p->d.double_data);
00528             break;
00529             
00530           case GATType_String:
00531             retval = GATTable_Add_String(parameters, p->key, p->d.string_data);
00532             break;
00533             
00534           default:
00535             retval = GAT_KEY_TYPE_ERROR;
00536             break;
00537         }
00538       
00539         if (GAT_SUCCESS != retval)
00540         {
00541           break;
00542         }
00543       }
00544       
00545       if (GAT_SUCCESS == retval)
00546       {    
00547         /* create the metric to store into the list */
00548         GATMetric new_metric = GATMetric_Create(data->name, parameters, 
00549           data->measurement_type, data->data_type, data->unit);
00550     
00551         if (NULL == new_metric)
00552         {
00553           retval = GAT_MEMORYFAILURE;
00554         }
00555         else
00556         {
00557           *metric = new_metric;
00558         }
00559       }
00560       GATTable_Destroy(&parameters);
00561     }
00562   }
00563   return retval;
00564 }
00565 
00566 /** GATMetric_CreateListOfMetrics
00567  *
00568  *  The function GATMetric_CreateListOfMetrics creates a list of GATMetric 
00569  *  objects from a given list of static metric data. This is a pure helper 
00570  *  function for adaptor writers.
00571  *
00572  *  @param data The address of the static metric data array.
00573  *  @param count The size of the given array of static metric data.
00574  *  @param list The pointer to the variable, which receives the resulting list
00575  *        of metrics.
00576  *
00577  *  @return An error code.
00578  */
00579 GATResult
00580   GATMetric_CreateListOfMetrics(GATStaticMetric *data, GATuint32 count,
00581     GATList_GATMetric *list)
00582 {
00583   GATResult retval = GAT_INVALID_PARAMETER;
00584   if (NULL != data && NULL != list)
00585   {
00586     GATuint32 i = 0;
00587     GATList_GATMetric new_list = GATList_GATMetric_Create();
00588     
00589     *list = NULL;
00590     if (NULL == new_list)
00591     {
00592       retval = GAT_MEMORYFAILURE;
00593     }
00594     else
00595     {
00596       for (/**/; i < count; ++i)
00597       {
00598         GATList_GATMetric_Iterator it = NULL;
00599         GATMetric metric = NULL;
00600         
00601         retval = GATMetric_CreateMetric(&data[i], &metric);
00602         if (GAT_SUCCESS != retval)
00603         {
00604           break;
00605         }
00606         
00607         /* store the metric into the list */
00608         it = GATList_GATMetric_Insert(new_list, 
00609           GATList_GATMetric_End(new_list), metric);
00610           
00611         GATMetric_Destroy(&metric);
00612         if (NULL == it)
00613         {
00614           retval = GAT_MEMORYFAILURE;
00615           break;
00616         }
00617       }
00618       
00619       if (GAT_SUCCESS == retval)
00620       {
00621         *list = new_list;
00622       }
00623     }
00624   }
00625   return retval;
00626 }
00627 
00628 /* Local functions */