@cryptotaxi247 / netdata-1 / commits / 26fe27e9f

Fix Locks (Windows Locks) (#20131)

thiagoftsm committed Apr 16, 2025 at 06:41 UTC 26fe27e9f65c54c1e37e5859f71e6c224e932bbd
1 file changed +53 -50
src/collectors/windows.plugin/perflib-mssql.c
+53 -50
@@ -67,9 +67,9 @@ struct mssql_instance {
67 RRDSET *st_sql_errors;
68 RRDDIM *rd_sql_errors;
69
70 - RRDSET *st_lockWait;
71 - RRDSET *st_deadLocks;
70 DICTIONARY *locks_instances;
71 + RRDSET *st_deadLocks;
72 + RRDSET *st_lockWait;
73
74 DICTIONARY *databases;
75
@@ -105,23 +105,14 @@ struct mssql_instance {
105 COUNTER_DATA MSSQLRecompilations;
106 };
107
108 -enum lock_instance_idx {
109 - NETDATA_MSSQL_ENUM_MLI_IDX_WAIT,
110 - NETDATA_MSSQL_ENUM_MLI_IDX_DEAD_LOCKS,
111 -
112 - NETDATA_MSSQL_ENUM_MLI_IDX_END
113 -};
114 -
108 struct mssql_lock_instance {
116 - struct mssql_instance *parent;
109 + char *resourceID;
110
111 COUNTER_DATA lockWait;
112 COUNTER_DATA deadLocks;
113
114 RRDDIM *rd_lockWait;
115 RRDDIM *rd_deadLocks;
123 -
124 - uint32_t updated;
116 };
117
118 enum db_instance_idx {
@@ -244,8 +235,11 @@ static inline void initialize_mssql_keys(struct mssql_instance *p)
235
236 void dict_mssql_insert_locks_cb(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused)
237 {
238 + const char *resource = dictionary_acquired_item_name((DICTIONARY_ITEM *)item);
239 +
240 // https://learn.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-locks-object
241 struct mssql_lock_instance *ptr = value;
242 + ptr->resourceID = strdupz(resource);
243 ptr->deadLocks.key = "Number of Deadlocks/sec";
244 ptr->lockWait.key = "Lock Waits/sec";
245 }
@@ -783,19 +777,14 @@ static void do_mssql_errors(PERF_DATA_BLOCK *pDataBlock, struct mssql_instance *
777 }
778 }
779
786 -int dict_mssql_locks_charts_cb(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused)
780 +void dict_mssql_locks_wait_charts(struct mssql_instance *mi, int update_every)
781 {
788 - char id[RRD_ID_LENGTH_MAX + 1];
782 + if (!mi->st_lockWait) {
783 + char id[RRD_ID_LENGTH_MAX + 1];
784
790 - struct mssql_lock_instance *mli = value;
791 - const char *instance = dictionary_acquired_item_name((DICTIONARY_ITEM *)item);
792 -
793 - int *update_every = data;
794 -
795 - if (!mli->parent->st_lockWait) {
796 - snprintfz(id, RRD_ID_LENGTH_MAX, "instance_%s_locks_lock_wait", mli->parent->instanceID);
785 + snprintfz(id, RRD_ID_LENGTH_MAX, "instance_%s_locks_lock_wait", mi->instanceID);
786 netdata_fix_chart_name(id);
798 - mli->parent->st_lockWait = rrdset_create_localhost(
787 + mi->st_lockWait = rrdset_create_localhost(
788 "mssql",
789 id,
790 NULL,
@@ -806,26 +795,35 @@ int dict_mssql_locks_charts_cb(const DICTIONARY_ITEM *item __maybe_unused, void
795 PLUGIN_WINDOWS_NAME,
796 "PerflibMSSQL",
797 PRIO_MSSQL_LOCKS_WAIT,
809 - *update_every,
798 + update_every,
799 RRDSET_TYPE_LINE);
800
801 rrdlabels_add(
813 - mli->parent->st_lockWait->rrdlabels, "mssql_instance", mli->parent->instanceID, RRDLABEL_SRC_AUTO);
802 + mi->st_lockWait->rrdlabels, "mssql_instance", mi->instanceID, RRDLABEL_SRC_AUTO);
803 }
804 +}
805
806 +void dict_mssql_locks_wait_dimension(struct mssql_instance *mi, struct mssql_lock_instance *mli)
807 +{
808 if (!mli->rd_lockWait) {
817 - mli->rd_lockWait = rrddim_add(mli->parent->st_lockWait, instance, NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
818 - }
809 + char id[RRD_ID_LENGTH_MAX + 1];
810 + snprintfz(id, RRD_ID_LENGTH_MAX, "%s", mli->resourceID);
811 + netdata_fix_chart_name(id);
812
820 - if (mli->updated & (1 << NETDATA_MSSQL_ENUM_MLI_IDX_WAIT)) {
821 - rrddim_set_by_pointer(
822 - mli->parent->st_lockWait, mli->rd_lockWait, (collected_number)(mli->lockWait.current.Data));
813 + mli->rd_lockWait = rrddim_add(mi->st_lockWait, id, NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
814 }
815 + rrddim_set_by_pointer(
816 + mi->st_lockWait, mli->rd_lockWait, (collected_number)(mli->lockWait.current.Data));
817 +}
818 +
819 +void dict_mssql_dead_locks_charts(struct mssql_instance *mi, int update_every)
820 +{
821 + if (!mi->st_deadLocks) {
822 + char id[RRD_ID_LENGTH_MAX + 1];
823
825 - if (!mli->parent->st_deadLocks) {
826 - snprintfz(id, RRD_ID_LENGTH_MAX, "instance_%s_locks_deadlocks", mli->parent->instanceID);
824 + snprintfz(id, RRD_ID_LENGTH_MAX, "instance_%s_locks_deadlocks", mi->instanceID);
825 netdata_fix_chart_name(id);
828 - mli->parent->st_deadLocks = rrdset_create_localhost(
826 + mi->st_deadLocks = rrdset_create_localhost(
827 "mssql",
828 id,
829 NULL,
@@ -836,23 +834,25 @@ int dict_mssql_locks_charts_cb(const DICTIONARY_ITEM *item __maybe_unused, void
834 PLUGIN_WINDOWS_NAME,
835 "PerflibMSSQL",
836 PRIO_MSSQL_LOCKS_DEADLOCK,
839 - *update_every,
837 + update_every,
838 RRDSET_TYPE_LINE);
839
840 rrdlabels_add(
843 - mli->parent->st_deadLocks->rrdlabels, "mssql_instance", mli->parent->instanceID, RRDLABEL_SRC_AUTO);
841 + mi->st_deadLocks->rrdlabels, "mssql_instance", mi->instanceID, RRDLABEL_SRC_AUTO);
842 }
843 +}
844
845 +void dict_mssql_deadlocks_dimension(struct mssql_instance *mi, struct mssql_lock_instance *mli)
846 +{
847 if (!mli->rd_deadLocks) {
847 - mli->rd_deadLocks = rrddim_add(mli->parent->st_deadLocks, instance, NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
848 - }
848 + char id[RRD_ID_LENGTH_MAX + 1];
849 + snprintfz(id, RRD_ID_LENGTH_MAX, "%s", mli->resourceID);
850 + netdata_fix_chart_name(id);
851
850 - if (mli->updated & (1 << NETDATA_MSSQL_ENUM_MLI_IDX_DEAD_LOCKS)) {
851 - rrddim_set_by_pointer(
852 - mli->parent->st_deadLocks, mli->rd_deadLocks, (collected_number)mli->deadLocks.current.Data);
852 + mli->rd_deadLocks = rrddim_add(mi->st_deadLocks, id, NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
853 }
854 -
855 - return 1;
854 + rrddim_set_by_pointer(
855 + mi->st_deadLocks, mli->rd_deadLocks, (collected_number)mli->deadLocks.current.Data);
856 }
857
858 static void do_mssql_locks(PERF_DATA_BLOCK *pDataBlock, struct mssql_instance *p, int update_every)
@@ -861,6 +861,11 @@ static void do_mssql_locks(PERF_DATA_BLOCK *pDataBlock, struct mssql_instance *p
861 if (!pObjectType)
862 return;
863
864 + if (!pObjectType->NumInstances)
865 + return;
866 +
867 + dict_mssql_locks_wait_charts(p, update_every);
868 + dict_mssql_dead_locks_charts(p, update_every);
869 PERF_INSTANCE_DEFINITION *pi = NULL;
870 for (LONG i = 0; i < pObjectType->NumInstances; i++) {
871 pi = perflibForEachInstance(pDataBlock, pObjectType, pi);
@@ -877,20 +882,18 @@ static void do_mssql_locks(PERF_DATA_BLOCK *pDataBlock, struct mssql_instance *p
882 if (!mli)
883 continue;
884
880 - if (!mli->parent) {
881 - mli->parent = p;
882 - }
883 -
885 if (perflibGetObjectCounter(pDataBlock, pObjectType, &mli->lockWait))
885 - mli->updated |= (1 << NETDATA_MSSQL_ENUM_MLI_IDX_WAIT);
886 + dict_mssql_locks_wait_dimension(p, mli);
887
888 if (perflibGetObjectCounter(pDataBlock, pObjectType, &mli->deadLocks))
888 - mli->updated |= (1 << NETDATA_MSSQL_ENUM_MLI_IDX_DEAD_LOCKS);
889 + dict_mssql_deadlocks_dimension(p, mli);
890 }
891
891 - dictionary_sorted_walkthrough_read(p->locks_instances, dict_mssql_locks_charts_cb, &update_every);
892 - rrdset_done(p->st_lockWait);
893 - rrdset_done(p->st_deadLocks);
892 + if (p->st_lockWait)
893 + rrdset_done(p->st_lockWait);
894 +
895 + if (p->st_deadLocks)
896 + rrdset_done(p->st_deadLocks);
897 }
898
899 static void mssql_database_backup_restore_chart(struct mssql_db_instance *mli, const char *db, int update_every)