| 1 | /* |
| 2 | Copyright(C) 2019-2022 Sutou Kouhei <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #include "grn_ctx.h" |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | #ifdef WIN32 |
| 24 | /* For Windows Server 2018 or earlier. */ |
| 25 | # define PSAPI_VERSION 1 |
| 26 | # include <psapi.h> |
| 27 | #endif |
| 28 | |
| 29 | uint64_t |
| 30 | grn_memory_get_usage(grn_ctx *ctx) |
| 31 | { |
| 32 | #ifdef WIN32 |
| 33 | PROCESS_MEMORY_COUNTERS_EX counters; |
| 34 | if (!GetProcessMemoryInfo(GetCurrentProcess(), |
| 35 | (PPROCESS_MEMORY_COUNTERS)&counters, |
| 36 | sizeof(counters))) { |
| 37 | SERR("GetProcessMemoryInfo"); |
| 38 | return 0; |
| 39 | } |
| 40 | return counters.PrivateUsage; |
| 41 | #elif defined(HAVE_GETRUSAGE) |
| 42 | struct rusage usage; |
| 43 | if (getrusage(RUSAGE_SELF, &usage) != 0) { |
| 44 | SERR("getrusage"); |
| 45 | return 0; |
| 46 | } |
| 47 | return (uint64_t)(usage.ru_maxrss) * 1024; |
| 48 | #else |
| 49 | return 0; |
| 50 | #endif |
| 51 | } |