-
Notifications
You must be signed in to change notification settings - Fork 130
Report resource usage at the end #125
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
Open
pasis
wants to merge
1
commit into
Mellanox:sockperf_v2
Choose a base branch
from
pasis:rusage
base: sockperf_v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,114 @@ | ||
| /* | ||
| * Copyright (c) 2011-2019 Mellanox Technologies Ltd. | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without modification, | ||
| * are permitted provided that the following conditions are met: | ||
| * | ||
| * 1. Redistributions of source code must retain the above copyright notice, | ||
| * this list of conditions and the following disclaimer. | ||
| * 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| * this list of conditions and the following disclaimer in the documentation | ||
| * and/or other materials provided with the distribution. | ||
| * 3. Neither the name of the Mellanox Technologies Ltd nor the names of its | ||
| * contributors may be used to endorse or promote products derived from this | ||
| * software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | ||
| * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
| * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT | ||
| * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT | ||
| * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
| * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY | ||
| * OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| #include "resources.h" | ||
| #include "defs.h" | ||
|
|
||
| #include <stdio.h> | ||
|
|
||
| #ifndef WIN32 | ||
| #include <sys/types.h> | ||
| #include <sys/time.h> | ||
| #include <sys/resource.h> | ||
| #else | ||
| #include <string.h> | ||
| #endif | ||
|
|
||
| #define TV_TO_USEC(tv) ((tv)->tv_sec * 1000000ULL + (tv)->tv_usec) | ||
| #define SAFE_SUB(x, y) ((y) < (x) ? (x) - (y) : 0) | ||
|
|
||
| static unsigned long long startup_timestamp; | ||
|
|
||
| void resources_init(void) | ||
| { | ||
| int rc = 0; | ||
| #ifndef WIN32 | ||
| struct timeval tv; | ||
|
|
||
| rc = gettimeofday(&tv, NULL); | ||
| if (rc == 0) | ||
| startup_timestamp = TV_TO_USEC(&tv); | ||
| #endif /* WIN32*/ | ||
|
|
||
| if (rc != 0) | ||
| throw("resources_init failed"); | ||
| } | ||
|
|
||
| void resources_get(resources_t *res) | ||
| { | ||
| int rc = 0; | ||
| #ifndef WIN32 | ||
| struct rusage r; | ||
| struct timeval tv; | ||
|
|
||
| #ifdef RUSAGE_THREAD | ||
| /* RUSAGE_THREAD appeared in Linux 2.6.26, MacOS manual misses it too. */ | ||
| rc = getrusage(RUSAGE_THREAD, &r); | ||
| #else | ||
| rc = getrusage(RUSAGE_SELF, &r); | ||
| #endif | ||
| if (rc == 0) { | ||
| res->utime = TV_TO_USEC(&r.ru_utime); | ||
| res->stime = TV_TO_USEC(&r.ru_stime); | ||
| res->nvcsw = r.ru_nvcsw; | ||
| res->nivcsw = r.ru_nivcsw; | ||
| rc = gettimeofday(&tv, NULL); | ||
| if (rc == 0) { | ||
| res->timestamp = TV_TO_USEC(&tv); | ||
| res->rtime = SAFE_SUB(res->timestamp, startup_timestamp); | ||
| } | ||
| } | ||
| #else | ||
| memset(res, 0, sizeof *res); | ||
| #endif /* WIN32 */ | ||
|
|
||
| if (rc != 0) | ||
| throw("resources_get failed"); | ||
| } | ||
|
|
||
| void resources_sub(resources_t *res1, resources_t *res2, resources_t *result) | ||
| { | ||
| result->timestamp = res1->timestamp; | ||
| result->rtime = SAFE_SUB(res1->timestamp, res2->timestamp); | ||
| result->stime = SAFE_SUB(res1->stime, res2->stime); | ||
| result->utime = SAFE_SUB(res1->utime, res2->utime); | ||
| result->nvcsw = SAFE_SUB(res1->nvcsw, res2->nvcsw); | ||
| result->nivcsw = SAFE_SUB(res1->nivcsw, res2->nivcsw); | ||
| } | ||
|
|
||
| void resources_print(resources_t *res) | ||
| { | ||
| #ifndef WIN32 | ||
| double cpu = 0.0; | ||
|
|
||
| if (res->rtime != 0) | ||
| cpu = (double)(res->stime + res->utime) / (double)res->rtime; | ||
| log_msg("Resources usage: CPU=%.2f%%, nvcsw=%lu, nivcsw=%lu", | ||
| cpu * 100.0, res->nvcsw, res->nivcsw); | ||
| #endif /* WIN32 */ | ||
| } |
This file contains hidden or 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 (c) 2011-2019 Mellanox Technologies Ltd. | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without modification, | ||
| * are permitted provided that the following conditions are met: | ||
| * | ||
| * 1. Redistributions of source code must retain the above copyright notice, | ||
| * this list of conditions and the following disclaimer. | ||
| * 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| * this list of conditions and the following disclaimer in the documentation | ||
| * and/or other materials provided with the distribution. | ||
| * 3. Neither the name of the Mellanox Technologies Ltd nor the names of its | ||
| * contributors may be used to endorse or promote products derived from this | ||
| * software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | ||
| * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
| * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT | ||
| * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT | ||
| * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
| * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY | ||
| * OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| /** | ||
| * @file resources.h | ||
| * | ||
| * @details: Resources usage statistics | ||
| * | ||
| * @author Dmytro Podgornyi <[email protected]> | ||
| * reviewed by | ||
| * | ||
| **/ | ||
|
|
||
| #ifndef _RESOURCES_H_ | ||
| #define _RESOURCES_H_ | ||
|
|
||
| typedef struct resources_t { | ||
| /** Timestamp, epoch time */ | ||
| unsigned long long timestamp; | ||
| /** Run time */ | ||
| unsigned long long rtime; | ||
| /** System time (how long CPU spent in kernelspace) */ | ||
| unsigned long long stime; | ||
| /** User time (how long CPU spent in userspace) */ | ||
| unsigned long long utime; | ||
| /** Voluntary context switches */ | ||
| unsigned long nvcsw; | ||
| /** Involuntary context switches */ | ||
| unsigned long nivcsw; | ||
| } resources_t; | ||
|
|
||
| /** | ||
| * Initializes resources subsystem. Must be called at startup. | ||
| */ | ||
| void resources_init(void); | ||
|
|
||
| /** | ||
| * Stores counters since the start of the process. | ||
| */ | ||
| void resources_get(resources_t *res); | ||
|
|
||
| /** | ||
| * result = res1 - res2. | ||
| * Finds how much resources are used within the period between res2 and res1. | ||
| */ | ||
| void resources_sub(resources_t *res1, resources_t *res2, resources_t *result); | ||
|
|
||
| /** | ||
| * Logs resources usage in human readable format. res can be produced by both | ||
| * resources_get() and resources_sub(). | ||
| */ | ||
| void resources_print(resources_t *res); | ||
|
|
||
| #endif /* _RESOURCES_H_ */ |
This file contains hidden or 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
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.
m_rusagecan be local