-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't leak or race in FreeBSD devstat collector #396
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
20ca0f1
Eliminate memory leak in FreeBSD devstat collector
dominikh 5e220c1
Move cgo portions of FreeBSD devstat collector into own file
dominikh ea55d0f
Don't race in FreeBSD devstat collector
dominikh 38c5890
Reuse devinfo struct
dominikh 9847257
Add missing license headers
dominikh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2017 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build !nodevstat | ||
|
||
#include <devstat.h> | ||
#include <fcntl.h> | ||
#include <libgeom.h> | ||
#include <limits.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <devstat_freebsd.h> | ||
|
||
|
||
int _get_stats(struct devinfo *info, Stats **stats) { | ||
struct statinfo current; | ||
current.dinfo = info; | ||
|
||
if (devstat_getdevs(NULL, ¤t) == -1) { | ||
return -1; | ||
} | ||
|
||
Stats *p = (Stats*)calloc(current.dinfo->numdevs, sizeof(Stats)); | ||
for (int i = 0; i < current.dinfo->numdevs; i++) { | ||
uint64_t bytes_read, bytes_write, bytes_free; | ||
uint64_t transfers_other, transfers_read, transfers_write, transfers_free; | ||
long double duration_other, duration_read, duration_write, duration_free; | ||
long double busy_time; | ||
uint64_t blocks; | ||
|
||
strcpy(p[i].device, current.dinfo->devices[i].device_name); | ||
p[i].unit = current.dinfo->devices[i].unit_number; | ||
devstat_compute_statistics(¤t.dinfo->devices[i], | ||
NULL, | ||
1.0, | ||
DSM_TOTAL_BYTES_READ, &bytes_read, | ||
DSM_TOTAL_BYTES_WRITE, &bytes_write, | ||
DSM_TOTAL_BYTES_FREE, &bytes_free, | ||
DSM_TOTAL_TRANSFERS_OTHER, &transfers_other, | ||
DSM_TOTAL_TRANSFERS_READ, &transfers_read, | ||
DSM_TOTAL_TRANSFERS_WRITE, &transfers_write, | ||
DSM_TOTAL_TRANSFERS_FREE, &transfers_free, | ||
DSM_TOTAL_DURATION_OTHER, &duration_other, | ||
DSM_TOTAL_DURATION_READ, &duration_read, | ||
DSM_TOTAL_DURATION_WRITE, &duration_write, | ||
DSM_TOTAL_DURATION_FREE, &duration_free, | ||
DSM_TOTAL_BUSY_TIME, &busy_time, | ||
DSM_TOTAL_BLOCKS, &blocks, | ||
DSM_NONE); | ||
|
||
p[i].bytes.read = bytes_read; | ||
p[i].bytes.write = bytes_write; | ||
p[i].bytes.free = bytes_free; | ||
p[i].transfers.other = transfers_other; | ||
p[i].transfers.read = transfers_read; | ||
p[i].transfers.write = transfers_write; | ||
p[i].transfers.free = transfers_free; | ||
p[i].duration.other = duration_other; | ||
p[i].duration.read = duration_read; | ||
p[i].duration.write = duration_write; | ||
p[i].duration.free = duration_free; | ||
p[i].busyTime = busy_time; | ||
p[i].blocks = blocks; | ||
} | ||
|
||
*stats = p; | ||
return current.dinfo->numdevs; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2017 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <devstat.h> | ||
#include <fcntl.h> | ||
#include <libgeom.h> | ||
#include <limits.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
typedef struct { | ||
uint64_t read; | ||
uint64_t write; | ||
uint64_t free; | ||
} Bytes; | ||
|
||
typedef struct { | ||
uint64_t other; | ||
uint64_t read; | ||
uint64_t write; | ||
uint64_t free; | ||
} Transfers; | ||
|
||
typedef struct { | ||
double other; | ||
double read; | ||
double write; | ||
double free; | ||
} Duration; | ||
|
||
typedef struct { | ||
char device[DEVSTAT_NAME_LEN]; | ||
int unit; | ||
Bytes bytes; | ||
Transfers transfers; | ||
Duration duration; | ||
long busyTime; | ||
uint64_t blocks; | ||
} Stats; | ||
|
||
|
||
int _get_ndevs(); | ||
int _get_stats(struct devinfo *info, Stats **stats); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please include our license header at the top.