/src/fluent-bit/plugins/in_gpu_metrics/gpu_common.c
Line | Count | Source |
1 | | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* Fluent Bit |
4 | | * ========== |
5 | | * Copyright (C) 2015-2026 The Fluent Bit Authors |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <stdio.h> |
21 | | #include <stdlib.h> |
22 | | #include <string.h> |
23 | | #include <inttypes.h> |
24 | | #include <errno.h> |
25 | | #include <limits.h> |
26 | | |
27 | | #include <fluent-bit/flb_input_plugin.h> |
28 | | #include <fluent-bit/flb_mem.h> |
29 | | |
30 | | #include "gpu_common.h" |
31 | | #include "gpu_metrics.h" |
32 | | |
33 | | int gpu_read_uint64(const char *path, uint64_t *value) |
34 | 0 | { |
35 | 0 | FILE *fp; |
36 | |
|
37 | 0 | fp = fopen(path, "r"); |
38 | 0 | if (!fp) { |
39 | 0 | return -1; |
40 | 0 | } |
41 | 0 | if (fscanf(fp, "%" PRIu64, value) != 1) { |
42 | 0 | fclose(fp); |
43 | 0 | return -1; |
44 | 0 | } |
45 | 0 | fclose(fp); |
46 | 0 | return 0; |
47 | 0 | } |
48 | | |
49 | | int gpu_read_double(const char *path, double scale, double *value) |
50 | 0 | { |
51 | 0 | uint64_t tmp; |
52 | 0 | if (gpu_read_uint64(path, &tmp) != 0) { |
53 | 0 | return -1; |
54 | 0 | } |
55 | 0 | *value = (double) tmp / scale; |
56 | 0 | return 0; |
57 | 0 | } |
58 | | |
59 | | int gpu_read_line(const char *path, char *buf, size_t size) |
60 | 0 | { |
61 | 0 | FILE *fp; |
62 | 0 | char *nl; |
63 | |
|
64 | 0 | fp = fopen(path, "r"); |
65 | 0 | if (!fp) { |
66 | 0 | return -1; |
67 | 0 | } |
68 | 0 | if (!fgets(buf, size, fp)) { |
69 | 0 | fclose(fp); |
70 | 0 | return -1; |
71 | 0 | } |
72 | | // Remove newline |
73 | 0 | nl = strchr(buf, '\n'); |
74 | 0 | if (nl) { |
75 | 0 | *nl = '\0'; |
76 | 0 | } |
77 | 0 | fclose(fp); |
78 | 0 | return 0; |
79 | 0 | } |
80 | | |
81 | | static int match_card_pattern(const char *pattern, int card_id) |
82 | 0 | { |
83 | 0 | char *dup; |
84 | 0 | char *token; |
85 | 0 | char *saveptr; |
86 | 0 | int start; |
87 | 0 | int end; |
88 | |
|
89 | 0 | if (!pattern || pattern[0] == '\0' || strcmp(pattern, "*") == 0) { |
90 | 0 | return FLB_TRUE; |
91 | 0 | } |
92 | | |
93 | 0 | dup = flb_strdup(pattern); |
94 | 0 | if (!dup) { |
95 | 0 | return FLB_FALSE; |
96 | 0 | } |
97 | | |
98 | 0 | token = strtok_r(dup, ",", &saveptr); |
99 | 0 | while (token) { |
100 | 0 | if (sscanf(token, "%d-%d", &start, &end) == 2) { |
101 | 0 | if (card_id >= start && card_id <= end) { |
102 | 0 | flb_free(dup); |
103 | 0 | return FLB_TRUE; |
104 | 0 | } |
105 | 0 | } |
106 | 0 | else { |
107 | 0 | char *endptr; |
108 | 0 | long parsed_id; |
109 | |
|
110 | 0 | errno = 0; |
111 | 0 | parsed_id = strtol(token, &endptr, 10); |
112 | 0 | if (errno == 0 && |
113 | 0 | endptr != token && |
114 | 0 | *endptr == '\0' && |
115 | 0 | parsed_id >= INT_MIN && |
116 | 0 | parsed_id <= INT_MAX && |
117 | 0 | card_id == (int) parsed_id) { |
118 | 0 | flb_free(dup); |
119 | 0 | return FLB_TRUE; |
120 | 0 | } |
121 | 0 | } |
122 | 0 | token = strtok_r(NULL, ",", &saveptr); |
123 | 0 | } |
124 | 0 | flb_free(dup); |
125 | 0 | return FLB_FALSE; |
126 | 0 | } |
127 | | |
128 | | int gpu_should_include_card(struct in_gpu_metrics *ctx, int card_id) |
129 | 0 | { |
130 | 0 | if (ctx->cards_exclude && ctx->cards_exclude[0] != '\0' && |
131 | 0 | match_card_pattern(ctx->cards_exclude, card_id)) { |
132 | 0 | flb_plg_info(ctx->ins, "card%d excluded by exclude pattern", card_id); |
133 | 0 | return FLB_FALSE; |
134 | 0 | } |
135 | | |
136 | 0 | if (ctx->cards_include && ctx->cards_include[0] != '\0' && |
137 | 0 | !match_card_pattern(ctx->cards_include, card_id)) { |
138 | 0 | flb_plg_info(ctx->ins, "card%d excluded by include pattern", card_id); |
139 | 0 | return FLB_FALSE; |
140 | 0 | } |
141 | | |
142 | 0 | return FLB_TRUE; |
143 | 0 | } |