1 /*
2    (C) 2001 by Argonne National Laboratory.
3        See COPYRIGHT in top-level directory.
4 */
5 #include "mpe_logging_conf.h"
6 
7 #if defined( STDC_HEADERS ) || defined( HAVE_STDIO_H )
8 #include <stdio.h>
9 #endif
10 #if defined( STDC_HEADERS ) || defined( HAVE_STDLIB_H )
11 #include <stdlib.h>
12 #endif
13 #if defined( STDC_HEADERS ) || defined( HAVE_STRING_H )
14 #include <string.h>
15 #endif
16 #if defined( HAVE_UNISTD_H )
17 #include <unistd.h>
18 #endif
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
21 #endif
22 
23 #if !defined( CLOG_NOMPI )
24 #include "mpi.h"
25 #else
26 #include "mpi_null.h"
27 #endif /* Endof if !defined( CLOG_NOMPI ) */
28 
29 /*
30    Definitions for CLOG's Universal Unique ID implementation
31 */
32 #include "clog_const.h"
33 #include "clog_uuid.h"
34 #include "clog_util.h"
35 
36 
37 #if defined( NEEDS_SRAND48_DECL )
38 void srand48(long seedval);
39 #endif
40 #if defined( NEEDS_LRAND48_DECL )
41 long lrand48(void);
42 #endif
43 
44 
45 
46 static char CLOG_UUID_NULL_NAME[ CLOG_UUID_NAME_SIZE ]  = {0};
47 /* const  char CLOG_UUID_NULL[ CLOG_UUID_SIZE ]            = {0}; */
48 const  CLOG_Uuid_t CLOG_UUID_NULL                       = {0};
49 
CLOG_Uuid_init(void)50 void CLOG_Uuid_init( void )
51 {
52 #ifdef HAVE_WINDOWS_H
53     srand(getpid());
54 #else
55     pid_t  proc_pid;
56 
57     proc_pid = getpid();
58     srand48( (long) proc_pid );
59 #endif
60 }
61 
CLOG_Uuid_finalize(void)62 void CLOG_Uuid_finalize( void )
63 {}
64 
CLOG_Uuid_generate(CLOG_Uuid_t uuid)65 void CLOG_Uuid_generate( CLOG_Uuid_t uuid )
66 {
67     CLOG_int32_t  random_number;
68     double        time;
69     int           namelen;
70     char         *ptr;
71     char          processor_name[ MPI_MAX_PROCESSOR_NAME ] = {0};
72 
73 #ifdef HAVE_WINDOWS_H
74     random_number  = rand();
75 #else
76     random_number  = (CLOG_int32_t) lrand48();
77 #endif
78 
79     /* Can't use CLOG_Timer_get() as CLOG_Timer_start() has been called yet */
80     time  = PMPI_Wtime();
81 
82     PMPI_Get_processor_name( processor_name, &namelen );
83 
84     ptr  = &uuid[0];
85     memcpy( ptr, &random_number, sizeof(CLOG_int32_t) );
86     ptr += sizeof(CLOG_int32_t);
87     memcpy( ptr, &time, sizeof(double) );
88     ptr += sizeof(double);
89     if ( namelen < CLOG_UUID_NAME_SIZE ) {
90         memcpy( ptr, processor_name, namelen );
91         /* pad the rest of uuid with 0 */
92         ptr += namelen;
93         memcpy( ptr, CLOG_UUID_NULL_NAME, CLOG_UUID_NAME_SIZE-namelen );
94     }
95     else /* if ( namelen >= CLOG_UUID_NAME_SIZE ) */
96         memcpy( ptr, processor_name, CLOG_UUID_NAME_SIZE );
97 }
98 
99 /*
100    Assume the output string array, str, is big enough to hold
101    the string representation of the CLOG_Uuid_t
102 */
CLOG_Uuid_sprint(CLOG_Uuid_t uuid,char * str)103 void CLOG_Uuid_sprint( CLOG_Uuid_t uuid, char *str )
104 {
105     CLOG_int32_t  random_number;
106     double        time;
107     char          name[ CLOG_UUID_NAME_SIZE+1 ] = {0};
108     char         *ptr;
109 
110     ptr  = &uuid[0];
111     memcpy( &random_number, ptr, sizeof(CLOG_int32_t) );
112     ptr += sizeof(CLOG_int32_t);
113     memcpy( &time, ptr, sizeof(double) );
114     ptr += sizeof(double);
115     memcpy( &name, ptr, CLOG_UUID_NAME_SIZE );
116     sprintf( str, i32fmt"-%f-%s", random_number, time, name );
117 }
118 
CLOG_Uuid_is_equal(const CLOG_Uuid_t uuid1,const CLOG_Uuid_t uuid2)119 int  CLOG_Uuid_is_equal( const CLOG_Uuid_t uuid1, const CLOG_Uuid_t uuid2 )
120 {
121      if ( memcmp( uuid1, uuid2, CLOG_UUID_SIZE ) == 0 )
122          return CLOG_BOOL_TRUE;
123      else
124          return CLOG_BOOL_FALSE;
125 }
126 
CLOG_Uuid_compare(const void * obj1,const void * obj2)127 int  CLOG_Uuid_compare( const void *obj1, const void *obj2 )
128 {
129     return memcmp( obj1, obj2, CLOG_UUID_SIZE );
130 }
131 
CLOG_Uuid_copy(const CLOG_Uuid_t src_uuid,CLOG_Uuid_t dest_uuid)132 void CLOG_Uuid_copy( const CLOG_Uuid_t src_uuid, CLOG_Uuid_t dest_uuid )
133 {
134     memcpy( dest_uuid, src_uuid, CLOG_UUID_SIZE );
135 }
136 
CLOG_Uuid_swap_bytes(CLOG_Uuid_t uuid)137 void CLOG_Uuid_swap_bytes( CLOG_Uuid_t uuid )
138 {
139     char   *ptr;
140 
141     ptr  = &uuid[0];
142     CLOG_Util_swap_bytes( ptr, sizeof(CLOG_int32_t), 1 );
143     ptr += sizeof(CLOG_int32_t);
144     CLOG_Util_swap_bytes( ptr, sizeof(double), 1 );
145 }
146