|
21 | 21 | #include <pthread.h> |
22 | 22 | #include <string.h> |
23 | 23 |
|
| 24 | +#include "comdb2.h" |
24 | 25 | #include "logmsg.h" |
| 26 | +#include "sql.h" |
25 | 27 |
|
26 | 28 | #include "api_history.h" |
27 | 29 |
|
28 | | -struct api_history { |
29 | | - hash_t *entries; |
30 | | - pthread_rwlock_t lock; |
31 | | -}; |
32 | | - |
33 | 30 | static int hash_entry(api_driver_t *entry, int len) |
34 | 31 | { |
35 | 32 | return hash_default_fixedwidth((unsigned char*)entry->name, strlen(entry->name)); |
@@ -73,101 +70,55 @@ static int free_entry(api_driver_t *entry, void *arg) |
73 | 70 | return 0; |
74 | 71 | } |
75 | 72 |
|
76 | | -void acquire_api_history_lock(api_history_t *api_history, int write) |
77 | | -{ |
78 | | - if (write) { |
79 | | - Pthread_rwlock_wrlock(&api_history->lock); |
80 | | - } else { |
81 | | - Pthread_rwlock_rdlock(&api_history->lock); |
82 | | - } |
83 | | -} |
84 | | - |
85 | | -void release_api_history_lock(api_history_t *api_history) |
86 | | -{ |
87 | | - Pthread_rwlock_unlock(&api_history->lock); |
88 | | -} |
89 | | - |
90 | 73 | api_history_t *init_api_history() |
91 | 74 | { |
92 | | - api_history_t *api_history = malloc(sizeof(api_history_t)); |
93 | | - assert(api_history); |
94 | | - Pthread_rwlock_init(&api_history->lock, NULL); |
95 | | - |
96 | | - api_history->entries = hash_init_user((hashfunc_t *)hash_entry, (cmpfunc_t *)compare_entries, 0, 0); |
97 | | - if (!api_history->entries) { |
| 75 | + api_history_t *api_history = hash_init_user((hashfunc_t *)hash_entry, (cmpfunc_t *)compare_entries, 0, 0); |
| 76 | + if (!api_history) { |
98 | 77 | logmsg(LOGMSG_FATAL, "Unable to initialize api history.\n"); |
99 | 78 | abort(); |
100 | 79 | } |
101 | | - |
| 80 | + |
102 | 81 | return api_history; |
103 | 82 | } |
104 | 83 |
|
105 | | -int free_api_history(api_history_t *api_history) |
| 84 | +void free_api_history(api_history_t *api_history) |
106 | 85 | { |
107 | 86 | assert(api_history); |
108 | | - acquire_api_history_lock(api_history, 1); |
109 | | - |
110 | | - int rc = hash_for(api_history->entries, (hashforfunc_t *)free_entry, NULL); |
111 | | - if (rc) { |
112 | | - logmsg(LOGMSG_FATAL, "Unable to free api history entries.\n"); |
113 | | - release_api_history_lock(api_history); |
114 | | - return rc; |
115 | | - } |
116 | | - |
117 | | - hash_free(api_history->entries); |
118 | | - release_api_history_lock(api_history); |
119 | | - Pthread_rwlock_destroy(&api_history->lock); |
120 | | - free(api_history); |
121 | | - return 0; |
| 87 | + hash_for(api_history, (hashforfunc_t *)free_entry, NULL); |
| 88 | + hash_free(api_history); |
122 | 89 | } |
123 | 90 |
|
124 | | -api_driver_t *get_next_api_history_entry(api_history_t *api_history, void **curr, unsigned int *iter) |
| 91 | +int get_num_api_history_entries(struct rawnodestats *stats) |
125 | 92 | { |
126 | | - assert(api_history); |
127 | | - acquire_api_history_lock(api_history, 0); |
128 | | - api_driver_t *entry; |
129 | | - |
130 | | - if (!*curr && !*iter) { |
131 | | - entry = (api_driver_t *)hash_first(api_history->entries, curr, iter); |
132 | | - } else { |
133 | | - entry = (api_driver_t *)hash_next(api_history->entries, curr, iter); |
134 | | - } |
135 | | - |
136 | | - release_api_history_lock(api_history); |
137 | | - return entry; |
138 | | -} |
139 | | - |
140 | | -int get_num_api_history_entries(api_history_t *api_history) { |
141 | | - assert(api_history); |
142 | | - acquire_api_history_lock(api_history, 0); |
143 | | - |
144 | | - int num = hash_get_num_entries(api_history->entries); |
145 | | - |
146 | | - release_api_history_lock(api_history); |
| 93 | + assert(stats); |
| 94 | + Pthread_mutex_lock(&stats->lk); |
| 95 | + int num = stats->api_history ? hash_get_num_entries(stats->api_history) : 0; |
| 96 | + Pthread_mutex_unlock(&stats->lk); |
147 | 97 | return num; |
148 | 98 | } |
149 | 99 |
|
150 | | -int update_api_history(api_history_t *api_history, char *api_driver_name, char *api_driver_version) |
| 100 | +int update_api_history(struct sqlclntstate *clnt) |
151 | 101 | { |
152 | | - assert(api_history); |
153 | | - acquire_api_history_lock(api_history, 1); |
154 | | - |
155 | | - api_driver_t *search_entry = create_entry(api_driver_name, api_driver_version); |
156 | | - api_driver_t *found_entry = hash_find(api_history->entries, search_entry); |
157 | | - |
| 102 | + assert(clnt && clnt->rawnodestats); |
| 103 | + struct rawnodestats *stats = clnt->rawnodestats; |
| 104 | + |
| 105 | + Pthread_mutex_lock(&stats->lk); |
| 106 | + api_driver_t *search_entry = create_entry(clnt->api_driver_name, clnt->api_driver_version); |
| 107 | + api_driver_t *found_entry = hash_find(stats->api_history, search_entry); |
| 108 | + |
158 | 109 | if (found_entry) { |
159 | 110 | time(&found_entry->last_seen); |
160 | 111 | free_entry(search_entry, NULL); |
161 | 112 | } else { |
162 | 113 | time(&search_entry->last_seen); |
163 | | - int rc = hash_add(api_history->entries, search_entry); |
| 114 | + int rc = hash_add(stats->api_history, search_entry); |
164 | 115 | if (rc) { |
| 116 | + Pthread_mutex_unlock(&stats->lk); |
165 | 117 | logmsg(LOGMSG_FATAL, "Unable to add api history entry.\n"); |
166 | | - release_api_history_lock(api_history); |
167 | 118 | return rc; |
168 | 119 | } |
169 | 120 | } |
170 | 121 |
|
171 | | - release_api_history_lock(api_history); |
| 122 | + Pthread_mutex_unlock(&stats->lk); |
172 | 123 | return 0; |
173 | 124 | } |
0 commit comments