|
| 1 | +/* |
| 2 | + * Copyright Redis Ltd. 2016 - present |
| 3 | + * Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or |
| 4 | + * the Server Side Public License v1 (SSPLv1). |
| 5 | + */ |
| 6 | + |
| 7 | +#include "global_stats.h" |
| 8 | +#include "aggregate/aggregate.h" |
| 9 | +#include "util/units.h" |
| 10 | + |
| 11 | +#define INCR_BY(x,y) __atomic_add_fetch(&(x), (y), __ATOMIC_RELAXED) |
| 12 | +#define INCR(x) INCR_BY(x, 1) |
| 13 | +#define READ(x) __atomic_load_n(&(x), __ATOMIC_RELAXED) |
| 14 | + |
| 15 | +GlobalStats RSGlobalStats = {0}; |
| 16 | +size_t FieldIndexErrorCounter[INDEXFLD_NUM_TYPES] = {0}; |
| 17 | + |
| 18 | +// Assuming that the GIL is already acquired |
| 19 | +void FieldsGlobalStats_UpdateStats(FieldSpec *fs, int toAdd) { |
| 20 | + if (fs->types & INDEXFLD_T_FULLTEXT) { // text field |
| 21 | + RSGlobalStats.fieldsStats.numTextFields += toAdd; |
| 22 | + } else if (fs->types & INDEXFLD_T_NUMERIC) { // numeric field |
| 23 | + RSGlobalStats.fieldsStats.numNumericFields += toAdd; |
| 24 | + } else if (fs->types & INDEXFLD_T_GEO) { // geo field |
| 25 | + RSGlobalStats.fieldsStats.numGeoFields += toAdd; |
| 26 | + } else if (fs->types & INDEXFLD_T_VECTOR) { // vector field |
| 27 | + RSGlobalStats.fieldsStats.numVectorFields += toAdd; |
| 28 | + if (fs->vectorOpts.vecSimParams.algo == VecSimAlgo_BF) |
| 29 | + RSGlobalStats.fieldsStats.numVectorFieldsFlat += toAdd; |
| 30 | + else if (fs->vectorOpts.vecSimParams.algo == VecSimAlgo_TIERED) { |
| 31 | + if (fs->vectorOpts.vecSimParams.algoParams.tieredParams.primaryIndexParams->algo == VecSimAlgo_HNSWLIB) |
| 32 | + RSGlobalStats.fieldsStats.numVectorFieldsHNSW += toAdd; |
| 33 | + } |
| 34 | + } else if (fs->types & INDEXFLD_T_TAG) { // tag field |
| 35 | + RSGlobalStats.fieldsStats.numTagFields += toAdd; |
| 36 | + if (fs->tagOpts.tagFlags & TagField_CaseSensitive) { |
| 37 | + RSGlobalStats.fieldsStats.numTagFieldsCaseSensitive += toAdd; |
| 38 | + } |
| 39 | + } else if (fs->types & INDEXFLD_T_GEOMETRY) { // geometry field |
| 40 | + RSGlobalStats.fieldsStats.numGeometryFields += toAdd; |
| 41 | + } |
| 42 | + |
| 43 | + if (fs->options & FieldSpec_Sortable) { |
| 44 | + if (fs->types & INDEXFLD_T_FULLTEXT) RSGlobalStats.fieldsStats.numTextFieldsSortable += toAdd; |
| 45 | + else if (fs->types & INDEXFLD_T_NUMERIC) RSGlobalStats.fieldsStats.numNumericFieldsSortable += toAdd; |
| 46 | + else if (fs->types & INDEXFLD_T_GEO) RSGlobalStats.fieldsStats.numGeoFieldsSortable += toAdd; |
| 47 | + else if (fs->types & INDEXFLD_T_TAG) RSGlobalStats.fieldsStats.numTagFieldsSortable += toAdd; |
| 48 | + else if (fs->types & INDEXFLD_T_GEOMETRY) RSGlobalStats.fieldsStats.numGeometryFieldsSortable += toAdd; |
| 49 | + } |
| 50 | + if (fs->options & FieldSpec_NotIndexable) { |
| 51 | + if (fs->types & INDEXFLD_T_FULLTEXT) RSGlobalStats.fieldsStats.numTextFieldsNoIndex += toAdd; |
| 52 | + else if (fs->types & INDEXFLD_T_NUMERIC) RSGlobalStats.fieldsStats.numNumericFieldsNoIndex += toAdd; |
| 53 | + else if (fs->types & INDEXFLD_T_GEO) RSGlobalStats.fieldsStats.numGeoFieldsNoIndex += toAdd; |
| 54 | + else if (fs->types & INDEXFLD_T_TAG) RSGlobalStats.fieldsStats.numTagFieldsNoIndex += toAdd; |
| 55 | + else if (fs->types & INDEXFLD_T_GEOMETRY) RSGlobalStats.fieldsStats.numGeometryFieldsNoIndex += toAdd; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +void FieldsGlobalStats_UpdateIndexError(FieldType field_type, int toAdd) { |
| 60 | + FieldIndexErrorCounter[INDEXTYPE_TO_POS(field_type)] += toAdd; |
| 61 | +} |
| 62 | + |
| 63 | +size_t FieldsGlobalStats_GetIndexErrorCount(FieldType field_type) { |
| 64 | + return FieldIndexErrorCounter[INDEXTYPE_TO_POS(field_type)]; |
| 65 | +} |
| 66 | + |
| 67 | +void TotalGlobalStats_CountQuery(uint32_t reqflags, clock_t duration) { |
| 68 | + if (reqflags & QEXEC_F_INTERNAL) return; // internal queries are not counted |
| 69 | + |
| 70 | + INCR(RSGlobalStats.totalStats.queries.total_query_commands); |
| 71 | + INCR_BY(RSGlobalStats.totalStats.queries.total_query_execution_time, duration); |
| 72 | + |
| 73 | + if (!(QEXEC_F_IS_CURSOR & reqflags) || (QEXEC_F_IS_AGGREGATE & reqflags)) { |
| 74 | + // Count only unique queries, not iterations of a previous query (FT.CURSOR READ) |
| 75 | + INCR(RSGlobalStats.totalStats.queries.total_queries_processed); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +QueriesGlobalStats TotalGlobalStats_GetQueryStats() { |
| 80 | + QueriesGlobalStats stats = {0}; |
| 81 | + stats.total_queries_processed = READ(RSGlobalStats.totalStats.queries.total_queries_processed); |
| 82 | + stats.total_query_commands = READ(RSGlobalStats.totalStats.queries.total_query_commands); |
| 83 | + stats.total_query_execution_time = READ(RSGlobalStats.totalStats.queries.total_query_execution_time) / CLOCKS_PER_MILLISEC; |
| 84 | + return stats; |
| 85 | +} |
| 86 | + |
| 87 | +void IndexsGlobalStats_UpdateLogicallyDeleted(int64_t toAdd) { |
| 88 | + INCR_BY(RSGlobalStats.totalStats.logically_deleted, toAdd); |
| 89 | +} |
| 90 | + |
| 91 | +size_t IndexesGlobalStats_GetLogicallyDeletedDocs() { |
| 92 | + return READ(RSGlobalStats.totalStats.logically_deleted); |
| 93 | +} |
0 commit comments