123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172(*****************************************************************************)(* Open Source License *)(* Copyright (c) 2018 Nomadic Labs, <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openMemoryopenError_monadtypeError_monad.error+=Unix_system_info_failureofstringlet()=Error_monad.register_error_kind`Temporary~id:"unix.system_info"~title:"Unix System_info failure"~description:"Unix System_info failure"~pp:(funppfs->Format.fprintfppf"@[<v 2>Unix system_info failure %s@]"s)Data_encoding.(obj1(req"failure"string))(functionUnix_system_info_failures->Somes|_->None)(funs->Unix_system_info_failures)leterror_infoprocesserror=Unix_system_info_failure(Format.asprintf"Unix_system_info_failure (%s: %s)"processerror)typesysname=Linux|Darwin|Unknownofstringletuname()=Lwt.catch(fun()->letopenLwt_syntaxinlet*s=Lwt_process.with_process_in~env:[|"LC_ALL=C"|]("uname",[|"uname"|])(funpc->Lwt_io.read_linepc#stdout)inmatchswith|"Linux"->Lwt.return_okLinux|"Darwin"->Lwt.return_okDarwin|os->Lwt.return_ok(Unknownos))(function|exn->Lwt.return_error(error_info"uname"(Printexc.to_stringexn)))letpage_sizesysname=letopenLwt_result_syntaxinletget_conf_process=matchsysnamewith|Linux->Ok("getconf",[|"getconf";"PAGE_SIZE"|])|Darwin->Ok("pagesize",[|"pagesize"|])|Unknown_->Error(error_info"pagesize""Unknown unix system")inlet*?process=get_conf_processinLwt.catch(fun()->Lwt_process.with_process_inprocess~env:[|"LC_ALL=C"|](funpc->let*!ps=Lwt_io.read_linepc#stdoutinLwt.return_ok(int_of_stringps)))(function|exn->Lwt.return_error(error_info"pagesize"(Printexc.to_stringexn)))letlinux_statmpid=Lwt.catch(fun()->letopenLwt_syntaxinletfname=Format.asprintf"/proc/%d/statm"pidinlet*b=Lwt_unix.file_existsfnameinmatchbwith|true->Lwt_io.with_file~mode:Inputfname(funic->let*line=Lwt_io.read_lineicinmatchList.mapInt64.of_string@@TzString.split_no_empty' 'linewith|size::resident::shared::text::lib::data::dt::_->(let*r=page_sizeLinuxinmatchrwith|Errore->Lwt.return_errore|Okpage_size->Lwt.return_ok(Statm{page_size;size;resident;shared;text;lib;data;dt;}))|_->Lwt.return_error(error_info"procfs statm""Unexpected proc/<pid>/statm format"))|false->Lwt.return_error(error_info"procfs statm"(Format.asprintf"%s not found"fname)))(function|exn->Lwt.return_error(error_info"procfs statm"(Printexc.to_stringexn)))letdarwin_pspid=Lwt.catch(fun()->letopenLwt_syntaxinLwt_process.with_process_in~env:[|"LC_ALL=C"|]("ps",[|"ps";"-o";"pid,%mem,rss";"-p";string_of_intpid|])(funpc->let*o=Lwt_io.read_line_optpc#stdoutinmatchowith|None->Lwt.return_error(error_info"ps""Unexpected ps answer (1st line)")|Some_->((* first line is useless *)let*o=Lwt_io.read_line_optpc#stdoutinmatchowith|None->Lwt.return_error(error_info"ps""Unexpected ps answer (2nd line)")|Someps_stats->(matchTzString.split_no_empty' 'ps_statswith|_pid::mem::resident::_->(let*r=page_sizeDarwininmatchrwith|Errore->Lwt.return_errore|Okpage_size->Lwt.return_ok(Ps{page_size;mem=float_of_stringmem;resident=Int64.of_stringresident;}))|_->Lwt.return_error(error_info"ps""Unexpected answer")))))(function|exn->Lwt.return_error(error_info"ps"(Printexc.to_stringexn)))letmemory_stats()=letopenLwt_syntaxinletpid=Unix.getpid()inlet*r=uname()inmatchrwith|Errore->Lwt.return_errore|OkLinux->linux_statmpid|OkDarwin->darwin_pspid|_->Lwt.return_error(error_info"memory_stats""Unknown unix system")