sigar-test 0.7.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/NOTICE +117 -0
- data/README +2 -0
- data/Rakefile +105 -0
- data/bindings/SigarBuild.pm +301 -0
- data/bindings/SigarWrapper.pm +3025 -0
- data/bindings/ruby/extconf.rb +131 -0
- data/bindings/ruby/rbsigar.c +888 -0
- data/include/sigar.h +984 -0
- data/include/sigar_fileinfo.h +157 -0
- data/include/sigar_format.h +65 -0
- data/include/sigar_getline.h +18 -0
- data/include/sigar_log.h +80 -0
- data/include/sigar_private.h +429 -0
- data/include/sigar_ptql.h +53 -0
- data/include/sigar_util.h +197 -0
- data/src/os/aix/aix_sigar.c +2168 -0
- data/src/os/aix/sigar_os.h +73 -0
- data/src/os/darwin/Info.plist.in +27 -0
- data/src/os/darwin/darwin_sigar.c +3718 -0
- data/src/os/darwin/sigar_os.h +80 -0
- data/src/os/hpux/hpux_sigar.c +1361 -0
- data/src/os/hpux/sigar_os.h +49 -0
- data/src/os/linux/linux_sigar.c +2810 -0
- data/src/os/linux/sigar_os.h +82 -0
- data/src/os/solaris/get_mib2.c +321 -0
- data/src/os/solaris/get_mib2.h +127 -0
- data/src/os/solaris/kstats.c +181 -0
- data/src/os/solaris/procfs.c +97 -0
- data/src/os/solaris/sigar_os.h +224 -0
- data/src/os/solaris/solaris_sigar.c +2732 -0
- data/src/os/win32/peb.c +212 -0
- data/src/os/win32/sigar.rc.in +40 -0
- data/src/os/win32/sigar_os.h +685 -0
- data/src/os/win32/sigar_pdh.h +47 -0
- data/src/os/win32/win32_sigar.c +4109 -0
- data/src/sigar.c +2444 -0
- data/src/sigar_cache.c +253 -0
- data/src/sigar_fileinfo.c +815 -0
- data/src/sigar_format.c +696 -0
- data/src/sigar_getline.c +1849 -0
- data/src/sigar_ptql.c +1976 -0
- data/src/sigar_signal.c +216 -0
- data/src/sigar_util.c +1060 -0
- data/src/sigar_version.c.in +22 -0
- data/src/sigar_version_autoconf.c.in +22 -0
- data/version.properties +11 -0
- metadata +91 -0
    
        data/src/sigar_cache.c
    ADDED
    
    | @@ -0,0 +1,253 @@ | |
| 1 | 
            +
            /*
         | 
| 2 | 
            +
             * Copyright (c) 2004-2006 Hyperic, Inc.
         | 
| 3 | 
            +
             *
         | 
| 4 | 
            +
             * Licensed under the Apache License, Version 2.0 (the "License");
         | 
| 5 | 
            +
             * you may not use this file except in compliance with the License.
         | 
| 6 | 
            +
             * You may obtain a copy of the License at
         | 
| 7 | 
            +
             *
         | 
| 8 | 
            +
             *     http://www.apache.org/licenses/LICENSE-2.0
         | 
| 9 | 
            +
             *
         | 
| 10 | 
            +
             * Unless required by applicable law or agreed to in writing, software
         | 
| 11 | 
            +
             * distributed under the License is distributed on an "AS IS" BASIS,
         | 
| 12 | 
            +
             * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         | 
| 13 | 
            +
             * See the License for the specific language governing permissions and
         | 
| 14 | 
            +
             * limitations under the License.
         | 
| 15 | 
            +
             */
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            #include "sigar.h"
         | 
| 18 | 
            +
            #include "sigar_private.h"
         | 
| 19 | 
            +
            #include "sigar_util.h"
         | 
| 20 | 
            +
            #include <stdio.h>
         | 
| 21 | 
            +
            /*
         | 
| 22 | 
            +
             * hash table to cache values where key is a unique number
         | 
| 23 | 
            +
             * such as:
         | 
| 24 | 
            +
             *  pid -> some process data
         | 
| 25 | 
            +
             *  uid -> user name
         | 
| 26 | 
            +
             *  gid -> group name
         | 
| 27 | 
            +
             */
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            #define ENTRIES_SIZE(n) \
         | 
| 30 | 
            +
                (sizeof(sigar_cache_entry_t *) * (n))
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            /* wrap free() for use w/ dmalloc */
         | 
| 33 | 
            +
            static void free_value(void *ptr)
         | 
| 34 | 
            +
            {
         | 
| 35 | 
            +
                free(ptr);
         | 
| 36 | 
            +
            }
         | 
| 37 | 
            +
             | 
| 38 | 
            +
            sigar_cache_t *sigar_expired_cache_new(int size, sigar_uint64_t cleanup_period_millis, sigar_uint64_t entry_expire_period)
         | 
| 39 | 
            +
            {
         | 
| 40 | 
            +
                sigar_cache_t *table = malloc(sizeof(*table));
         | 
| 41 | 
            +
                table->count = 0;
         | 
| 42 | 
            +
                table->size = size;
         | 
| 43 | 
            +
                table->entries = malloc(ENTRIES_SIZE(size));
         | 
| 44 | 
            +
                memset(table->entries, '\0', ENTRIES_SIZE(size));
         | 
| 45 | 
            +
                table->free_value = free_value;
         | 
| 46 | 
            +
                table->cleanup_period_millis = cleanup_period_millis;
         | 
| 47 | 
            +
                table->last_cleanup_time = sigar_time_now_millis();
         | 
| 48 | 
            +
                table->entry_expire_period = entry_expire_period;
         | 
| 49 | 
            +
                return table;
         | 
| 50 | 
            +
            }
         | 
| 51 | 
            +
             | 
| 52 | 
            +
            sigar_cache_t *sigar_cache_new(int size)
         | 
| 53 | 
            +
            {
         | 
| 54 | 
            +
                return sigar_expired_cache_new(size, SIGAR_FIELD_NOTIMPL, SIGAR_FIELD_NOTIMPL);
         | 
| 55 | 
            +
            }
         | 
| 56 | 
            +
             | 
| 57 | 
            +
             | 
| 58 | 
            +
            /*#ifdef DEBUG_CACHE*/
         | 
| 59 | 
            +
            /* see how well entries are distributed */
         | 
| 60 | 
            +
            void sigar_cache_dump(sigar_cache_t *table)
         | 
| 61 | 
            +
            {
         | 
| 62 | 
            +
                int i;
         | 
| 63 | 
            +
                sigar_cache_entry_t **entries = table->entries;
         | 
| 64 | 
            +
                printf("table size %lu\n", (long)table->size); 
         | 
| 65 | 
            +
                printf("table count %lu\n", (long)table->count);
         | 
| 66 | 
            +
                
         | 
| 67 | 
            +
                for (i=0; i<table->size; i++) {
         | 
| 68 | 
            +
                    sigar_cache_entry_t *entry = *entries++;
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                    printf("|");
         | 
| 71 | 
            +
                    while (entry) {
         | 
| 72 | 
            +
                        printf("%lld", entry->id);
         | 
| 73 | 
            +
                        if (entry->next) {
         | 
| 74 | 
            +
                            printf(",");
         | 
| 75 | 
            +
                        }
         | 
| 76 | 
            +
                        entry = entry->next;
         | 
| 77 | 
            +
                    }
         | 
| 78 | 
            +
                }
         | 
| 79 | 
            +
                printf("\n");
         | 
| 80 | 
            +
                fflush(stdout);
         | 
| 81 | 
            +
            }
         | 
| 82 | 
            +
            /*#endif*/
         | 
| 83 | 
            +
             | 
| 84 | 
            +
            static void sigar_cache_rehash(sigar_cache_t *table)
         | 
| 85 | 
            +
            {
         | 
| 86 | 
            +
                int i;
         | 
| 87 | 
            +
                unsigned int new_size = table->count * 2 + 1;
         | 
| 88 | 
            +
                sigar_cache_entry_t **entries = table->entries;
         | 
| 89 | 
            +
                sigar_cache_entry_t **new_entries =
         | 
| 90 | 
            +
                    malloc(ENTRIES_SIZE(new_size));
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                memset(new_entries, '\0', ENTRIES_SIZE(new_size));
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                for (i=0; i<table->size; i++) {
         | 
| 95 | 
            +
                    sigar_cache_entry_t *entry = *entries++;
         | 
| 96 | 
            +
             | 
| 97 | 
            +
                    while (entry) {
         | 
| 98 | 
            +
                        sigar_cache_entry_t *next = entry->next;
         | 
| 99 | 
            +
                        sigar_uint64_t hash = entry->id % new_size;
         | 
| 100 | 
            +
             | 
| 101 | 
            +
                        entry->next = new_entries[hash];
         | 
| 102 | 
            +
                        new_entries[hash] = entry;
         | 
| 103 | 
            +
                        entry = next;
         | 
| 104 | 
            +
                    }
         | 
| 105 | 
            +
                }
         | 
| 106 | 
            +
             | 
| 107 | 
            +
                free(table->entries);
         | 
| 108 | 
            +
                table->entries = new_entries;
         | 
| 109 | 
            +
                table->size = new_size;
         | 
| 110 | 
            +
            }
         | 
| 111 | 
            +
             | 
| 112 | 
            +
            #define SIGAR_CACHE_IX(t, k) \
         | 
| 113 | 
            +
                t->entries + (k % t->size)
         | 
| 114 | 
            +
             | 
| 115 | 
            +
            void sigar_perform_cleanup_if_necessary(sigar_cache_t *table) { 
         | 
| 116 | 
            +
                sigar_uint64_t current_time;
         | 
| 117 | 
            +
            	int i;
         | 
| 118 | 
            +
            	sigar_cache_entry_t **entries;
         | 
| 119 | 
            +
                if (table->cleanup_period_millis == SIGAR_FIELD_NOTIMPL) {
         | 
| 120 | 
            +
            		/* no cleanup for this cache) */
         | 
| 121 | 
            +
            		return;
         | 
| 122 | 
            +
                }
         | 
| 123 | 
            +
                current_time = sigar_time_now_millis();
         | 
| 124 | 
            +
                if ((current_time - table->last_cleanup_time) < table->cleanup_period_millis) {
         | 
| 125 | 
            +
                    /* not enough time has passed since last cleanup */
         | 
| 126 | 
            +
                    return;
         | 
| 127 | 
            +
                }	
         | 
| 128 | 
            +
             | 
| 129 | 
            +
                /* performing cleanup */    
         | 
| 130 | 
            +
                entries = table->entries;
         | 
| 131 | 
            +
             | 
| 132 | 
            +
                table->last_cleanup_time = current_time;
         | 
| 133 | 
            +
                
         | 
| 134 | 
            +
                for (i=0; i<table->size; i++) {
         | 
| 135 | 
            +
                    sigar_cache_entry_t *entry, *ptr, *entry_prev=NULL, **entry_in_table;
         | 
| 136 | 
            +
                    entry_in_table = entries;
         | 
| 137 | 
            +
                    entry = *entries++;
         | 
| 138 | 
            +
             | 
| 139 | 
            +
                    while (entry) {
         | 
| 140 | 
            +
            			sigar_uint64_t period_with_no_access = current_time - entry->last_access_time;
         | 
| 141 | 
            +
                        ptr = entry->next;            
         | 
| 142 | 
            +
                        if (table->entry_expire_period < period_with_no_access) {
         | 
| 143 | 
            +
            		       /* no one acess this entry for too long - we can delete it */
         | 
| 144 | 
            +
            	           if (entry->value) {
         | 
| 145 | 
            +
                               table->free_value(entry->value);
         | 
| 146 | 
            +
                            } 
         | 
| 147 | 
            +
                            free(entry);
         | 
| 148 | 
            +
            		        table->count--;
         | 
| 149 | 
            +
                            if (entry_prev != NULL) {
         | 
| 150 | 
            +
                               entry_prev->next = ptr;
         | 
| 151 | 
            +
                            }
         | 
| 152 | 
            +
                            else {
         | 
| 153 | 
            +
                               /* removing first entry - head of list should point to next entry */               
         | 
| 154 | 
            +
                               *entry_in_table = ptr;
         | 
| 155 | 
            +
                            } 
         | 
| 156 | 
            +
                        }
         | 
| 157 | 
            +
                        else {
         | 
| 158 | 
            +
                          /* entry not expired - advance entry_prev to current entry*/
         | 
| 159 | 
            +
                          entry_prev = entry;
         | 
| 160 | 
            +
            	        }
         | 
| 161 | 
            +
                        entry = ptr;
         | 
| 162 | 
            +
                    }
         | 
| 163 | 
            +
                }
         | 
| 164 | 
            +
                if (table->count < (table->size/4)) {
         | 
| 165 | 
            +
            	/* hash table (the array size) too big for the amount of values it contains perform rehash */
         | 
| 166 | 
            +
                    sigar_cache_rehash(table);
         | 
| 167 | 
            +
                }
         | 
| 168 | 
            +
            }
         | 
| 169 | 
            +
             | 
| 170 | 
            +
               
         | 
| 171 | 
            +
             | 
| 172 | 
            +
             | 
| 173 | 
            +
            sigar_cache_entry_t *sigar_cache_find(sigar_cache_t *table,
         | 
| 174 | 
            +
                                                  sigar_uint64_t key)
         | 
| 175 | 
            +
            {
         | 
| 176 | 
            +
                sigar_cache_entry_t *entry, **ptr;
         | 
| 177 | 
            +
                sigar_perform_cleanup_if_necessary(table);
         | 
| 178 | 
            +
             | 
| 179 | 
            +
                for (ptr = SIGAR_CACHE_IX(table, key), entry = *ptr;
         | 
| 180 | 
            +
                     entry;
         | 
| 181 | 
            +
                     ptr = &entry->next, entry = *ptr)
         | 
| 182 | 
            +
                {
         | 
| 183 | 
            +
                    if (entry->id == key) {
         | 
| 184 | 
            +
                        entry->last_access_time = sigar_time_now_millis();
         | 
| 185 | 
            +
                        return entry;
         | 
| 186 | 
            +
                    }
         | 
| 187 | 
            +
                }
         | 
| 188 | 
            +
             | 
| 189 | 
            +
                return NULL;
         | 
| 190 | 
            +
            }
         | 
| 191 | 
            +
             | 
| 192 | 
            +
            /* create entry if it does not exist */
         | 
| 193 | 
            +
            sigar_cache_entry_t *sigar_cache_get(sigar_cache_t *table,
         | 
| 194 | 
            +
                                                 sigar_uint64_t key)
         | 
| 195 | 
            +
            {
         | 
| 196 | 
            +
                sigar_cache_entry_t *entry, **ptr;
         | 
| 197 | 
            +
                sigar_perform_cleanup_if_necessary(table);
         | 
| 198 | 
            +
             | 
| 199 | 
            +
                for (ptr = SIGAR_CACHE_IX(table, key), entry = *ptr;
         | 
| 200 | 
            +
                     entry;
         | 
| 201 | 
            +
                     ptr = &entry->next, entry = *ptr)
         | 
| 202 | 
            +
                {
         | 
| 203 | 
            +
                    if (entry->id == key) {
         | 
| 204 | 
            +
                        entry->last_access_time = sigar_time_now_millis();
         | 
| 205 | 
            +
                        return entry;
         | 
| 206 | 
            +
                    }
         | 
| 207 | 
            +
                }
         | 
| 208 | 
            +
             | 
| 209 | 
            +
                if (++table->count > table->size) {
         | 
| 210 | 
            +
                    sigar_cache_rehash(table);
         | 
| 211 | 
            +
             | 
| 212 | 
            +
                    for (ptr = SIGAR_CACHE_IX(table, key), entry = *ptr;
         | 
| 213 | 
            +
                         entry;
         | 
| 214 | 
            +
                         ptr = &entry->next, entry = *ptr)
         | 
| 215 | 
            +
                    {
         | 
| 216 | 
            +
                    }
         | 
| 217 | 
            +
                }
         | 
| 218 | 
            +
             | 
| 219 | 
            +
                *ptr = entry = malloc(sizeof(*entry));
         | 
| 220 | 
            +
                entry->id = key;
         | 
| 221 | 
            +
                entry->value = NULL;
         | 
| 222 | 
            +
                entry->next = NULL;
         | 
| 223 | 
            +
                entry->last_access_time = sigar_time_now_millis();
         | 
| 224 | 
            +
             | 
| 225 | 
            +
                return entry;
         | 
| 226 | 
            +
            }
         | 
| 227 | 
            +
             | 
| 228 | 
            +
            void sigar_cache_destroy(sigar_cache_t *table)
         | 
| 229 | 
            +
            {
         | 
| 230 | 
            +
                int i;
         | 
| 231 | 
            +
                sigar_cache_entry_t **entries = table->entries;
         | 
| 232 | 
            +
             | 
| 233 | 
            +
            #ifdef DEBUG_CACHE
         | 
| 234 | 
            +
                sigar_cache_dump(table);
         | 
| 235 | 
            +
            #endif
         | 
| 236 | 
            +
             | 
| 237 | 
            +
                for (i=0; i<table->size; i++) {
         | 
| 238 | 
            +
                    sigar_cache_entry_t *entry, *ptr;
         | 
| 239 | 
            +
                    entry = *entries++;
         | 
| 240 | 
            +
             | 
| 241 | 
            +
                    while (entry) {
         | 
| 242 | 
            +
                        if (entry->value) {
         | 
| 243 | 
            +
                            table->free_value(entry->value);
         | 
| 244 | 
            +
                        }
         | 
| 245 | 
            +
                        ptr = entry->next;
         | 
| 246 | 
            +
                        free(entry);
         | 
| 247 | 
            +
                        entry = ptr;
         | 
| 248 | 
            +
                    }
         | 
| 249 | 
            +
                }
         | 
| 250 | 
            +
             | 
| 251 | 
            +
                free(table->entries);
         | 
| 252 | 
            +
                free(table);
         | 
| 253 | 
            +
            }
         |