diff options
author | Stelios Fragkakis <52996999+stelfrag@users.noreply.github.com> | 2023-07-10 14:13:50 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-10 14:13:50 +0300 |
commit | b12edb1208554cdbb93fa3d4e9570bf4a9879e0f (patch) | |
tree | 3c257a4a7077a24e7c99478c3a7158540f4465c2 /database | |
parent | eb6f1de7c6f7b626cb2ec333697b63034115914b (diff) |
Use spinlock in host and chart (#15328)
* Switch alarm log lock to spinlock
* Switch the alerts lock in the chart structure to spinlock
* Proper lock usage
Diffstat (limited to 'database')
-rw-r--r-- | database/contexts/api_v2.c | 8 | ||||
-rw-r--r-- | database/contexts/query_target.c | 8 | ||||
-rw-r--r-- | database/rrd.h | 4 | ||||
-rw-r--r-- | database/rrdcalc.c | 16 | ||||
-rw-r--r-- | database/rrdhost.c | 1 | ||||
-rw-r--r-- | database/rrdset.c | 4 | ||||
-rw-r--r-- | database/sqlite/sqlite_aclk_alert.c | 14 | ||||
-rw-r--r-- | database/sqlite/sqlite_health.c | 4 |
8 files changed, 28 insertions, 31 deletions
diff --git a/database/contexts/api_v2.c b/database/contexts/api_v2.c index 5984d553b3..9c84a6fe9b 100644 --- a/database/contexts/api_v2.c +++ b/database/contexts/api_v2.c @@ -408,7 +408,7 @@ static FTS_MATCH rrdcontext_to_json_v2_full_text_search(struct rrdcontext_to_jso if(ri->rrdset) { RRDSET *st = ri->rrdset; - netdata_rwlock_rdlock(&st->alerts.rwlock); + rw_spinlock_read_lock(&st->alerts.spinlock); for (RRDCALC *rcl = st->alerts.base; rcl; rcl = rcl->next) { if(unlikely(full_text_search_string(&ctl->q.fts, q, rcl->name))) { matched = FTS_MATCHED_ALERT; @@ -420,7 +420,7 @@ static FTS_MATCH rrdcontext_to_json_v2_full_text_search(struct rrdcontext_to_jso break; } } - netdata_rwlock_unlock(&st->alerts.rwlock); + rw_spinlock_read_unlock(&st->alerts.spinlock); } } dfe_done(ri); @@ -433,7 +433,7 @@ static bool rrdcontext_matches_alert(struct rrdcontext_to_json_v2_data *ctl, RRD dfe_start_read(rc->rrdinstances, ri) { if(ri->rrdset) { RRDSET *st = ri->rrdset; - netdata_rwlock_rdlock(&st->alerts.rwlock); + rw_spinlock_read_lock(&st->alerts.spinlock); for (RRDCALC *rcl = st->alerts.base; rcl; rcl = rcl->next) { if(ctl->alerts.alert_name_pattern && !simple_pattern_matches_string(ctl->alerts.alert_name_pattern, rcl->name)) continue; @@ -491,7 +491,7 @@ static bool rrdcontext_matches_alert(struct rrdcontext_to_json_v2_data *ctl, RRD dictionary_set(ctl->alerts.alert_instances, key, &z, sizeof(z)); } } - netdata_rwlock_unlock(&st->alerts.rwlock); + rw_spinlock_read_unlock(&st->alerts.spinlock); } } dfe_done(ri); diff --git a/database/contexts/query_target.c b/database/contexts/query_target.c index defb5acdd1..508977ce7a 100644 --- a/database/contexts/query_target.c +++ b/database/contexts/query_target.c @@ -579,7 +579,7 @@ static void query_target_eval_instance_rrdcalc(QUERY_TARGET_LOCALS *qtl __maybe_ QUERY_NODE *qn, QUERY_CONTEXT *qc, QUERY_INSTANCE *qi) { RRDSET *st = rrdinstance_acquired_rrdset(qi->ria); if (st) { - netdata_rwlock_rdlock(&st->alerts.rwlock); + rw_spinlock_read_lock(&st->alerts.spinlock); for (RRDCALC *rc = st->alerts.base; rc; rc = rc->next) { switch(rc->status) { case RRDCALC_STATUS_CLEAR: @@ -610,7 +610,7 @@ static void query_target_eval_instance_rrdcalc(QUERY_TARGET_LOCALS *qtl __maybe_ break; } } - netdata_rwlock_unlock(&st->alerts.rwlock); + rw_spinlock_read_unlock(&st->alerts.spinlock); } } @@ -624,7 +624,7 @@ static bool query_target_match_alert_pattern(RRDINSTANCE_ACQUIRED *ria, SIMPLE_P BUFFER *wb = NULL; bool matched = false; - netdata_rwlock_rdlock(&st->alerts.rwlock); + rw_spinlock_read_lock(&st->alerts.spinlock); if (st->alerts.base) { for (RRDCALC *rc = st->alerts.base; rc; rc = rc->next) { SIMPLE_PATTERN_RESULT ret = simple_pattern_matches_string_extract(pattern, rc->name, NULL, 0); @@ -655,7 +655,7 @@ static bool query_target_match_alert_pattern(RRDINSTANCE_ACQUIRED *ria, SIMPLE_P break; } } - netdata_rwlock_unlock(&st->alerts.rwlock); + rw_spinlock_read_unlock(&st->alerts.spinlock); buffer_free(wb); return matched; diff --git a/database/rrd.h b/database/rrd.h index 3bb9d6d438..2f697c81df 100644 --- a/database/rrd.h +++ b/database/rrd.h @@ -886,7 +886,7 @@ struct rrdset { const RRDFAMILY_ACQUIRED *rrdfamily; // pointer to RRDFAMILY dictionary item, this chart belongs to struct { - netdata_rwlock_t rwlock; // protection for RRDCALC *base + RW_SPINLOCK spinlock; // protection for RRDCALC *base RRDCALC *base; // double linked list of RRDCALC related to this RRDSET } alerts; @@ -1106,7 +1106,7 @@ typedef struct alarm_log { unsigned int count; unsigned int max; ALARM_ENTRY *alarms; - netdata_rwlock_t alarm_log_rwlock; + RW_SPINLOCK spinlock; } ALARM_LOG; typedef struct health { diff --git a/database/rrdcalc.c b/database/rrdcalc.c index fe9da58018..60e24426b4 100644 --- a/database/rrdcalc.c +++ b/database/rrdcalc.c @@ -62,7 +62,7 @@ inline const char *rrdcalc_status2string(RRDCALC_STATUS status) { } uint32_t rrdcalc_get_unique_id(RRDHOST *host, STRING *chart, STRING *name, uint32_t *next_event_id, uuid_t *config_hash_id) { - netdata_rwlock_rdlock(&host->health_log.alarm_log_rwlock); + rw_spinlock_read_lock(&host->health_log.spinlock); // re-use old IDs, by looking them up in the alarm log ALARM_ENTRY *ae = NULL; @@ -89,7 +89,7 @@ uint32_t rrdcalc_get_unique_id(RRDHOST *host, STRING *chart, STRING *name, uint3 } } - netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock); + rw_spinlock_read_unlock(&host->health_log.spinlock); return alarm_id; } @@ -212,9 +212,9 @@ static void rrdcalc_link_to_rrdset(RRDSET *st, RRDCALC *rc) { rc->last_status_change = now_realtime_sec(); rc->rrdset = st; - netdata_rwlock_wrlock(&st->alerts.rwlock); + rw_spinlock_write_lock(&st->alerts.spinlock); DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(st->alerts.base, rc, prev, next); - netdata_rwlock_unlock(&st->alerts.rwlock); + rw_spinlock_write_unlock(&st->alerts.spinlock); if(rc->update_every < rc->rrdset->update_every) { netdata_log_error("Health alarm '%s.%s' has update every %d, less than chart update every %d. Setting alarm update frequency to %d.", rrdset_id(rc->rrdset), rrdcalc_name(rc), rc->update_every, rc->rrdset->update_every, rc->rrdset->update_every); @@ -362,12 +362,12 @@ static void rrdcalc_unlink_from_rrdset(RRDCALC *rc, bool having_ll_wrlock) { // unlink it if(!having_ll_wrlock) - netdata_rwlock_wrlock(&st->alerts.rwlock); + rw_spinlock_write_lock(&st->alerts.spinlock); DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(st->alerts.base, rc, prev, next); if(!having_ll_wrlock) - netdata_rwlock_unlock(&st->alerts.rwlock); + rw_spinlock_write_unlock(&st->alerts.spinlock); rc->rrdset = NULL; @@ -808,7 +808,7 @@ void rrdcalc_delete_alerts_not_matching_host_labels_from_all_hosts() { void rrdcalc_unlink_all_rrdset_alerts(RRDSET *st) { RRDCALC *rc, *last = NULL; - netdata_rwlock_wrlock(&st->alerts.rwlock); + rw_spinlock_write_lock(&st->alerts.spinlock); while((rc = st->alerts.base)) { if(last == rc) { netdata_log_error("RRDCALC: malformed list of alerts linked to chart - cannot cleanup - giving up."); @@ -827,7 +827,7 @@ void rrdcalc_unlink_all_rrdset_alerts(RRDSET *st) { } } - netdata_rwlock_unlock(&st->alerts.rwlock); + rw_spinlock_write_unlock(&st->alerts.spinlock); } void rrdcalc_delete_all(RRDHOST *host) { diff --git a/database/rrdhost.c b/database/rrdhost.c index afe64ddf64..168c0a6711 100644 --- a/database/rrdhost.c +++ b/database/rrdhost.c @@ -1260,7 +1260,6 @@ void rrdhost_free___while_having_rrd_wrlock(RRDHOST *host, bool force) { string_freez(host->health.health_default_recipient); string_freez(host->registry_hostname); simple_pattern_free(host->rrdpush_send_charts_matching); - netdata_rwlock_destroy(&host->health_log.alarm_log_rwlock); freez(host->node_id); rrdfamily_index_destroy(host); diff --git a/database/rrdset.c b/database/rrdset.c index c16c1ee845..4f66964063 100644 --- a/database/rrdset.c +++ b/database/rrdset.c @@ -143,7 +143,7 @@ static void rrdset_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, v | RRDSET_FLAG_SENDER_REPLICATION_FINISHED ; - netdata_rwlock_init(&st->alerts.rwlock); + rw_spinlock_init(&st->alerts.spinlock); if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || st->rrd_memory_mode == RRD_MEMORY_MODE_MAP) { if(!rrdset_memory_load_or_create_map_save(st, st->rrd_memory_mode)) { @@ -263,8 +263,6 @@ static void rrdset_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, v // ------------------------------------------------------------------------ // free it - netdata_rwlock_destroy(&st->alerts.rwlock); - string_freez(st->id); string_freez(st->name); string_freez(st->parts.id); diff --git a/database/sqlite/sqlite_aclk_alert.c b/database/sqlite/sqlite_aclk_alert.c index ac84eac206..4eda23bc6f 100644 --- a/database/sqlite/sqlite_aclk_alert.c +++ b/database/sqlite/sqlite_aclk_alert.c @@ -454,11 +454,11 @@ void sql_queue_existing_alerts_to_aclk(RRDHOST *host) sqlite3_stmt *res = NULL; int rc; - netdata_rwlock_wrlock(&host->health_log.alarm_log_rwlock); + rw_spinlock_write_lock(&host->health_log.spinlock); buffer_sprintf(sql, "delete from aclk_alert_%s; ", uuid_str); if (unlikely(db_execute(db_meta, buffer_tostring(sql)))) { - netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock); + rw_spinlock_write_unlock(&host->health_log.spinlock); buffer_free(sql); return; } @@ -472,7 +472,7 @@ void sql_queue_existing_alerts_to_aclk(RRDHOST *host) rc = sqlite3_prepare_v2(db_meta, buffer_tostring(sql), -1, &res, 0); if (rc != SQLITE_OK) { error_report("Failed to prepare statement when trying to queue existing alerts."); - netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock); + rw_spinlock_write_unlock(&host->health_log.spinlock); buffer_free(sql); return; } @@ -481,7 +481,7 @@ void sql_queue_existing_alerts_to_aclk(RRDHOST *host) if (unlikely(rc != SQLITE_OK)) { error_report("Failed to bind host_id for when trying to queue existing alerts."); sqlite3_finalize(res); - netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock); + rw_spinlock_write_unlock(&host->health_log.spinlock); buffer_free(sql); return; } @@ -495,7 +495,7 @@ void sql_queue_existing_alerts_to_aclk(RRDHOST *host) if (unlikely(rc != SQLITE_OK)) error_report("Failed to finalize statement to queue existing alerts, rc = %d", rc); - netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock); + rw_spinlock_write_unlock(&host->health_log.spinlock); buffer_free(sql); rrdhost_flag_set(host, RRDHOST_FLAG_ACLK_STREAM_ALERTS); @@ -888,7 +888,7 @@ void aclk_push_alert_snapshot_event(char *node_id __maybe_unused) char uuid_str[UUID_STR_LEN]; uuid_unparse_lower_fix(&host->host_uuid, uuid_str); - netdata_rwlock_rdlock(&host->health_log.alarm_log_rwlock); + rw_spinlock_read_lock(&host->health_log.spinlock); ALARM_ENTRY *ae = host->health_log.alarms; @@ -973,7 +973,7 @@ void aclk_push_alert_snapshot_event(char *node_id __maybe_unused) aclk_send_alarm_snapshot(snapshot_proto); } - netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock); + rw_spinlock_read_unlock(&host->health_log.spinlock); wc->alerts_snapshot_uuid = NULL; freez(claim_id); diff --git a/database/sqlite/sqlite_health.c b/database/sqlite/sqlite_health.c index cca6e051e9..c7a794f885 100644 --- a/database/sqlite/sqlite_health.c +++ b/database/sqlite/sqlite_health.c @@ -789,7 +789,7 @@ void sql_health_alarm_log_load(RRDHOST *host) { } foreach_rrdcalc_in_rrdhost_done(rc); - netdata_rwlock_rdlock(&host->health_log.alarm_log_rwlock); + rw_spinlock_read_lock(&host->health_log.spinlock); while (sqlite3_step_monitored(res) == SQLITE_ROW) { ALARM_ENTRY *ae = NULL; @@ -947,7 +947,7 @@ void sql_health_alarm_log_load(RRDHOST *host) { loaded++; } - netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock); + rw_spinlock_read_unlock(&host->health_log.spinlock); dictionary_destroy(all_rrdcalcs); all_rrdcalcs = NULL; |