ãã®ããŒããèªãã åŸãFreeBSDã®ãµãŒããŒã¢ããªã±ãŒã·ã§ã³ã§äºæããªãã¡ã¢ãªãªãŒã¯ãçºçããåŸã«äœãçµéšããªããã°ãªããªãããããããŸãã ãã®ãããªåé¡ãæ€åºããçŸä»£ã®æ段ã¯ãã®ç°å¢ã«ååšãããããã®æã匷åãªãã®ã
æšææ¥ã®ããååŸãZabbixã¯50å°ã®ãµãŒããŒã®ãã¡5å°ãã¹ã¯ããã»ã¯ã·ã§ã³ã®ã¹ããŒã¹äžè¶³ã®éç¥ãéä¿¡ããŸããã CPVDïŒç©ºãã¡ã¢ãªïŒã®ã°ã©ãã¯ãåé¡ã®èŠæš¡ãæ確ã«ç€ºããŠããŸãïŒå³åŽã®ããŠã³ãã¯ãã¹ã¯ããã§ã®æ··éã«ããã¡ã¢ãªã®è§£æŸã§ãïŒã 幞ããªããšã«ãéææ¥ãå ã«ãããé±æ«ã«èœã¡çããŠãã¹ãŠãä¿®æ£ã§ããŸãã ãã®ç¬éã誰ãåå ãèŠã€ããŠæé€ããã®ã«6æ¥ä»¥äžããããšã¯æ³åãããŠããŸããã§ããã
ãµãŒããŒã«ã€ããŠã 8 GBã®ã¡ã¢ãªãæèŒãã10幎ç®ãã11幎ç®ã®äžä»£ã®äžè¬çãªãµãŒããŒïŒã»ãŒå®å šã«åäžã§ãåããã©ã³ãã®ãã®ã§ãïŒã ãµãŒããŒã¯ã°ã«ãŒãã«åããããããŸããŸãªãŠãŒã¶ãŒã¢ã«ãŠã³ãã®ã»ãããæäŸããŸãã
ã¢ããªã±ãŒã·ã§ã³ã«ã€ããŠã åºåãµãŒããŒãC \ C ++ããžãã¯ãåããHTTPïŒlibh2oïŒãå€æ°ã®ãµãŒãããŒãã£ã©ã€ãã©ãªãå ±æã¡ã¢ãªå ã®æšæºC ++ã³ã³ããã®ãããªèªè»¢è»ãªã© çä¿¡èŠæ±ãåãå ¥ããè€æ°ã®ã¢ããã¹ããªãŒã ãµãŒããŒã«ãªãã€ã¬ã¯ãããå¿çã®ãªãŒã¯ã·ã§ã³ãéå¬ããŠãã¯ã©ã€ã¢ã³ãã«å¿çãè¿ããŸãã ãã¹ãŠãFreeBSD 11.0 \ 11.1ãæå¹ã«ããŸãã
誰ã®ããã§ããïŒ
ã³ãŒãããŒã¹ã®æåŸã®å€æŽã¯çŽ10æ¥åã§ãæè¿ã¯ã¡ã¢ãªã«ç®ç«ã£ãå€åã¯ãããŸããã§ããã 倧ãŸããªåæã«ãããæãå¯èœæ§ã®é«ãçç±ã®ãªã¹ããåŸãããŸããã
- çä¿¡/çºä¿¡ãªã¯ãšã¹ãã®è³ªçãŸãã¯éçç¹æ§ãå€æŽãããã¡ã¢ãªã®å²ãåœãŠ/解æŸã§ãšã©ãŒãçºçããŸããã
- å ±æã¡ã¢ãªã«ã³ã³ãããããèªè»¢è»ã äœããããŸããããªãå Žåã圌ãã¯åžžã«ç念ãåŒãèµ·ãããŸãã
- ãªãŒã¯ã¯ãªããããŒã¿ãå¢ããã ãã§ãäžæãåæ¢ããŸããã
- OS \ã©ã€ãã©ãªã®ã«ãŒãã«ãæŽæ°ããçµæã ããããããã©ã«ãã®èªåæŽæ°ã¯ãããŸãããäœããã®çš®é¡ã®Windowsã§ã¯ãããŸãããå¿ èŠãªãšãã«ãã€ã§ãæŽæ°ãããŒã«ã¢ããã ãããã«ãã·ã³ãåèµ·åã§ããŸãã æ°ã¶æåãè¿é£ãããžã§ã¯ãã®å€ãã®ãµãŒãã¹ã厩å£ããŸããã
- 誰ããæšçãããã¯ãŒã¯æ»æãå®è¡ããŠãªãŒããŒãããŒãåŒãèµ·ãããŸãã
- ã¡ã«ãããŠã³\ã¹ãã¯ã¿ãŒã ã¯ãïŒ ãã¡ããã§ãã æžéãæããŠæŽæ°ãè¡ã£ãããšã¯èŠããŠããŸããããä»æ¥ã®ãã®ãããªãã€ã³ãã«ã¯ãç·æ¥äºæ ãçºçããå Žæããªããã°ãªããŸããã
ãã ããèŠæ±/å¿çã®ç¹æ§ã¯å€æŽãããŠããŸããïŒå°ãªããšãåéããããã¹ãŠã®ã¡ããªãã¯ã§ïŒã ãã以äžããŒã¿ã¯ãªãããããã¯ãŒã¯ã¯æŽç¶ãšããŠãããå€ãã®ç©ºããªãœãŒã¹ãããããµãŒããŒã¯ããã«å¿çããŸã...å€éšç°å¢ã¯å€æŽãããŸãããïŒ
ã©ããã
æ°å¹Žåã«äŒŒããããªãã®ãè¡šé¢åããããåé¡ã解決ããã®ã«åœ¹ç«ã€ã»ãšãã©ãã¹ãŠã®ããŒã«ã¯å¿ããããã ç§ã¯ã»ãã®æ°æéã§ãããåãé€ãããã£ãã®ã§ãæåã®çŽ æŽãªè©Šã¿ã¯StackOverflowãžã®çããèŠã€ããããšã§ãã...åºæ¬çã«ã人ã ã¯Valgrindãšããã€ãã®æªç¥ã®å·¥èžåïŒæããã«èè èªèº«ïŒããŸãã¯VisualStudioã®ãã©ã°ã€ã³ïŒé¢é£ãªãïŒããå§ãããŸãã ã¯ã©ããã¯ã»ãšãã©ãã¹ãŠã®ãã®ã«èœã¡ãé©åã«åäœãå§ããããšãããããŸããã§ããïŒmemleaxãElectricFenceãªã©ïŒã詳现ã«ã€ããŠã¯èª¬æããŸããã
éäžã§ãæè¿ãªãªãŒã¹ããããã¹ãŠã®æ©èœãæãåºããŸãã幞ããªããšã«ãå æãäž»ãªæ©èœã®å€æŽã¯ã»ãšãã©ãããŸããã§ãã-GeoIPããŒã¹ã®ãã蟌ã¿ãªã©ã詳现ã«...
ã¯ã©ã€ã¢ã³ããšäžäœãµãŒããŒã1ã€ãã€åæããããšããŠããŸãïŒãã®æ¹æ³ã¯æåã®æ¹æ³ã®1ã€ã§ããããäœããã®çç±ã§çµæãåŸããããããŸããŸãªåŒ·åºŠã®çµã¿åããã§ãªãŒã¯ãçºçããçä¿¡èŠæ±ãžã®ç·åœ¢äŸåã®ã¿ã芳å¯ãããŸããïŒã
ããã«ã2ãæåã®ç£æ»ãŸã§ãå€ãããŒãžã§ã³ã«ããŒã«ããã¯ããè©Šã¿ãè¡ãããŸããã ããã§ã¡ã¢ãªããªãŒã¯ãç¶ããŸããã ä»ã®ã³ã³ããŒãã³ããšã®äºææ§ããªãããã以åã®ããŒãžã§ã³ã«ããŒã«ããã¯ããããšã¯ã§ããŸããã§ããã
äž»ãªè³ªåã¯ããªããããã®5ã€ã®ãµãŒããŒã§ããïŒ ãã¹ãŠã®ãã·ã³ã¯ã»ãŒåãã§ãïŒOSããŒãžã§ã³11 \ 11.1ã®éããé€ãïŒã ãã®åé¡ã¯ãç¹å®ã®ã¢ã«ãŠã³ãã°ã«ãŒãã§ã®ã¿çºçããŸãã ä»ã®äººã«ã¯ãã®ãããªåäœã®ãã³ãã¯ãããŸãããã€ãŸããçä¿¡èŠæ±ã«æ£ç¢ºã«äŸåããŠããå¿ èŠããããŸã...
åæ
çãªäœè«
ããŸããŸãªãããžã§ã¯ãã§ç¹°ãè¿ããªãŒã¯ã«ééããæãäžè¬çã§å¹æçãªæ²»çæ¹æ³-æ¬ é¥ã®ããã¢ããªã±ãŒã·ã§ã³ãå®æçã«åèµ·åããããš-ã«é¢ããå€ãã®æããã話ãå¶ç¶èããŸããã ã¯ããã¯ããããã¯éåžžã«è©å€ã®è¯ãäŒæ¥ã§é·å¹ŽåããŠããããšãããããŸããã ããã¯åžžã«ç§ã«ãšã£ãŠå®å
šãªã²ãŒã ã®ããã«æãããåé¡ãã©ããªã«å€§ãããŠããç§ã¯èªåã®äººçã§æ±ºããŠããã«å±ããŸããã ãã ãããã§ã«2æ¥ç®ã«ãcronã§æ¥ãã¹ãåèµ·åãç»é²ããå¿
èŠããããŸããã æ°æéããšã«ã¢ããªã±ãŒã·ã§ã³ãåèµ·åããã®ã¯ãããªãé¢åãªäœæ¥ã§ããïŒç¹ã«å€éïŒã
äœããã®çç±ã§ãç§ã¯ãã¹ãŠãçŸããè¡ããããã€ãŸãæ®éçãªæ段ã§ãªãŒã¯ãã€ã³ããèŠã€ããããšæããŸããã ãããããããã¯åæ段éã§ç¯ããäž»ãªééãã®1ã€ã§ããã
ããã§ãä»æ¥èª¬æãããŠããåé¡ã解決ããããã®æ®éçãªææžçã¯äœã§ããïŒ åºæ¬çã«ããããã¯malloc \ freeãžã®åŒã³åºããã©ãããããã¹ãŠã®ã¡ã¢ãªæäœãç£èŠãããµãŒãããŒãã£ã©ã€ãã©ãªã§ãã
valgrind
åé¡ãæ€åºããåªããæ¹æ³ã å®éãã»ãŒãã¹ãŠã®ã¿ã€ãïŒããã«ãªãªãŒã¹ãå¢çãè¶ããããªãŒã¯ãªã©ïŒããã£ããããŸãã ãã®ç©èªã¯ãããå§ãŸãåã«çµãã£ãŠããã§ãããã ããããvalgrindã«ã¯1ã€ã®åé¡ããããŸããè² è·ã®é«ãã¢ããªã±ãŒã·ã§ã³ã«ã¯ã»ãšãã©å®å šã«åœ¹ã«ç«ããªããšããããšã§ãã 次ã®ããã«èŠããŸããããã°ã©ã ã¯éåžžããã20ã50åé·ãèµ·åããŸãããããã§ãåäœããŸãããã¡ãããã»ãšãã©ã®ãªã¯ãšã¹ãã«ã¯ãã¿ã€ã ã¢ãŠãããŠã¿ã€ã ã¢ãŠãããæéã¯ãããŸããã CPUã³ã¢ã¯100ïŒ ã§ããŒããããŸãããã¢ããªã±ãŒã·ã§ã³ã¯æçšãªã¢ã¯ã·ã§ã³ãå®è¡ãããvalgrindä»®æ³ãã·ã³äžã®ãã¹ãŠã®ãªãœãŒã¹ãæ¶è²»ããŸãã ãã°ãèŠããšãæ£åžžã«éä¿¡ããããã¹ãŠã®ãªã¯ãšã¹ãã®äœããŒã»ã³ããã®æ²æšãªéšåãèŠã€ãããŸãã CtrlããŒãæŒããªããCããŒãæŒããšãéãè¯ããã°æ°åã§ãã°ã衚瀺ããããããã¹ãŠãèœã¡ãŸãïŒå€ãã®å Žåã2çªç®ã®ãã°ããŸãã¯ã»ãšãã©ç©ºã®ãã°ããããŸããïŒã äžè¬çã«ãããã¯é¢éžããŸããã§ããã
tcmalloc
ã©ã€ãã©ãªã¯google-perftoolsããŒãããã¢ã¯ã»ã¹ã§ããŸãã éçºè ã«ãããš ïŒ
ããã¯ãGoogleã§äœ¿çšããããŒããããã¡ã€ã©ãŒã§ããã»ãšãã©ã®ãã®ãããªããŒã«ãšåæ§ã«ãç°å¢å€æ°ïŒLD_PRELOADïŒã䜿çšããããã©ã€ãã©ãªèªäœãã³ã³ãã€ã«ããïŒ-ltcmallocïŒããšã«ããæ¥ç¶ãããŸãã ã©ã¡ãã®æ¹æ³ãæ©èœããŸããã§ããã ããã»ã¹ã§å¥ã®ããã»ã¹ãçºèŠãããŸãã-ã³ãŒãããéçã¡ãœããHeapLeakChecker :: NoGlobalLeaksïŒïŒãåŒã³åºããŸãã ããããäœããã®çç±ã§ãã©ã®ã©ã€ãã©ãªããŒãžã§ã³ã«ããšã¯ã¹ããŒããããŸããã§ããã åŸã§å€æ ïŒ
[FreeBSDäž] libtcmalloc.soã¯æ£åžžã«ãã«ãããããé«åºŠãªãtcmallocæ©èœã¯ãã¹ãŠãLinuxåºæã®ã³ãŒããæã€ãªãŒã¯ãã§ãã«ãŒãé€ããŠãã¹ãŠæ©èœããŸãã:(ããã«é²ãã§ãããŸãããã
ãªãã¡ã
umemããŒãããå©çšå¯èœã ã¡ã¢ãªã®åé¡ãæ€åºããã¹ããŒããªæ¹æ³ã ç¹ã«MDBãšã®çµã¿åãã㧠ã æ®å¿µãªããããããã¯ãã¹ãŠSolaris OSã§ã®ã¿å©çšå¯èœã§ãããFreeBSD MDB ã«ç§»æ€ããããšããå§ãããŸãã ã¢ããªã±ãŒã·ã§ã³ãèµ·åã§ããŸããã§ããã èµ·åæã«ãmainãåŒã³åºãåã«ãlibthr.soããcallocãåŒã³åºãããŸããlibthr.soã¯ãã§ã«libumemã«ãã£ãŠã€ã³ã¿ãŒã»ãããããŠããŸãã 次ã«ãlibumemã¯ã³ãŒãå ã®ã¹ã¬ããã§äœæ¥ãåæåããããšããŸãã ååž°-sã äžè¬ã«ãã¡ã€ã³ã³ãŒã«ã®ããªãåã®ã¹ã¿ãã¯ã¯æ¬¡ã®ããã«ãªããŸãã
åµãšé¶èã®å žåçãªåé¡ãããã¯ã©ã®ããã«ç§»åãããæ確ã§ã¯ãããŸããã ãã«ãã¹ã¬ãããã«ãããããšããã¢ã€ãã¢ã延æããããšã«ããŸããïŒæ®å¿µãªãããããŒã¹ãã®äŸåé¢ä¿ãšgetaddrinfoã®ã©ãããŒã§ã®ã¿äœ¿çšããŠããŸãïŒã ãŸãã誰ããéããåæ§ã®ãã±ããã§éçºè ïŒãŸã çããŠããå ŽåïŒã«æžã蟌ã¿ãç¶ããŸãã
dmalloc
åãååã®ããŒãããå©çšã§ããŸãã åªããããã¥ã¡ã³ãããããŸãã èµ·åæã®ã¹ã¿ãã¯ã¯é©ãã»ã©åã®ã±ãŒã¹ã«äŒŒãŠããŸãïŒ
(gdb) bt #0 0x0000000802c8783e in dmalloc_malloc () from /usr/local/lib/libdmallocthcxx.so.1 #1 0x0000000802c88623 in calloc () from /usr/local/lib/libdmallocthcxx.so.1 #2 0x00000008038a8594 in ?? () from /lib/libthr.so.3 #3 0x00000008038a98d4 in ?? () from /lib/libthr.so.3 #4 0x00000008038a58fa in pthread_mutex_lock () from /lib/libthr.so.3 #5 0x0000000802c87641 in ?? () from /usr/local/lib/libdmallocthcxx.so.1 #6 0x0000000802c87bb3 in ?? () from /usr/local/lib/libdmallocthcxx.so.1 #7 0x0000000802c8787a in dmalloc_malloc () from /usr/local/lib/libdmallocthcxx.so.1 #8 0x0000000802c88623 in calloc () from /usr/local/lib/libdmallocthcxx.so.1 #9 0x00000008038a8594 in ?? () from /lib/libthr.so.3 #10 0x00000008038a98d4 in ?? () from /lib/libthr.so.3 #11 0x00000008038a58fa in pthread_mutex_lock () from /lib/libthr.so.3 #12 0x0000000802c87641 in ?? () from /usr/local/lib/libdmallocthcxx.so.1 #13 0x0000000802c87bb3 in ?? () from /usr/local/lib/libdmallocthcxx.so.1 #14 0x0000000802c8787a in dmalloc_malloc ()
ããããããã§èè ã¯åé¡ãèªèããŠãããç¬èªã®åé¿çãæäŸããŠããŸãïŒèµ·åæã«ãç¡èŠããå¿ èŠãããmallocåŒã³åºãã®æ°ãèšå®ããŸãïŒååž°ãåé¿ããããïŒã
ããã°ã©ã ãããã«ã³ã¢ãã³ãããå Žåã¯äœãããããšãdmallocã©ã€ãã©ãªãå€ãäœãã«ããããããååž°çã§ãããšèšã£ãŠããå Žåã¯é«ãããããšãããã£ãŠãããããããããã®åé¡ãçºçããå¯èœæ§ããããŸããããã ã³ã¢ãã³ããšååž°ã®éã«ã¿ã°ãä»ããŠå€§åãªæ°åãæŸãããã®äºæ¥å šäœãåæããããšã決å®ãããŸããã
ãã®éããµãŒããŒã®ã¡ã¢ãªã¯ããã«éã䜿ãæãããå§ããŸããã å®å šã«é£ã¹ã5ã6æéåã®å Žåã1æéã§å®å šã«èšæ¶ã倱ãããŸãã cronã埮調æŽããå¿ èŠããããŸããïŒããã«ãåé¡ãçºçãããããµãŒããŒã®ã»ãããæé·ãå§ããŸãããããããæ°ãããµãŒããŒã®ã»ããã®ã¡ã¢ãªã¯24æé以å ã«äœ¿ãæãããŸãããããã«ãã»ãšãã©ã®ãã·ã³ã§ã¯ãç®ã«èŠããã¡ã¢ãªã®åé¡ã¯ãŸã£ãããããŸããã§ããã
åæã«ãDCã®ãšã³ãžãã¢ã¯ãããã®ããŒã¿ãå¢ããããšããçè«ããã¹ãããããã«ããµãŒããŒã®1ã€ã«è¿œå ã®ã¡ã¢ãªãèŠæ±ããŸããïŒããããªãŒã¯ã§ã¯ãªãããšã確èªããæåŸã®å¿ æ»ã®è©Šã¿ïŒã DCã¯ããã«è¿œå ã®8GBãæäŸããŸããããããã¯å€éã«å®å šã«é£ã¹ãããŸããã ãªãŒã¯ã®ååšãçã人ã¯ããŸããã§ããã
dtrace
å·é ·ãªã°ãã°ã«ãºã®éçšã§ãç¥ç§çãªDèšèªã®ç¥ç§çãªã¹ã¯ãªããããŸããŸãç»å Žãå§ããŸããããããã¯ãã¹ãŠãã»ãšãã©ãã¹ãŠã®ã³ãŒãã®åæãšãããã°ã«ãããIDDQDã®äžçš®ã§ããdtraceãšããéåžžã«ã¯ãŒã«ãªã·ã¹ãã ãŠãŒãã£ãªãã£çšã«èšèšãããŠããããšãããããŸããã ç§ã¯åœŒã«ã€ããŠå€ãã®ããšãèããŸããããæŠéã«ã¯äœ¿ããŸããã§ããã
ã€ãŸã ããšãã°ãmalloc \ freeãªã©ã®ç¹å®ã®libcé¢æ°ãžã®ãã¹ãŠã®åŒã³åºããä¿åãããããŒããé 眮ããéäžã§äžè¬çãªçµ±èšãåéããããã«æ°è¡ã®ã³ãŒãã§ååžå³ãäœæããããšãã§ããŸãã ããšãã°ã次ã®ããã«ïŒ
sudo dtrace -n 'pid$target::malloc:entry { @ = quantize(arg0); }' -p 15034
-å®è¡äžã®ããã»ã¹ã§å²ãåœãŠããããããã¯ã®ååžã調ã¹ãããšãã§ããŸãïŒäŸ-pid = 15034ïŒã æ°ç§ã®ç£èŠã§ã¢ããªã±ãŒã·ã§ã³ã®ååžã¯æ¬¡ã®ããã«ãªããŸãã
value ------------- Distribution ------------- count 2 | 0 4 | 1407 8 | 455 16 |@@ 35592 32 |@@@@@@@@@@@@@@@@ 239205 64 |@@@@@@@ 112358 128 |@@@@ 55813 256 |@@@@@@ 91368 512 |@ 17204 1024 |@ 19751 2048 |@@ 33310 4096 | 2082 8192 | 554 16384 | 15 32768 | 0 65536 | 3960 131072 | 0
ããã§ãã åã³ã³ãã€ã«ããã«ããã®ãã¹ãŠããªã³ã¶ãã©ã€ã§ïŒ ç§ãã¡ã¯ãã¹ãŠã®ç§å¯ã解ãæããããšã«éåžžã«è¿ããšããã«ããªããã°ãªããŸããã
ãŸããã¢ããªã±ãŒã·ã§ã³ãããšã¯ã¹ããŒããããé¢æ°ã®çµ±èšãåéããããšãã§ããŸãïŒ ãã ããã³ã³ãã€ã«ãªãã·ã§ã³-O1以äžã䜿çšãããšãã»ãšãã©ã®èå³æ·±ãé¢æ°ããšã¯ã¹ããŒãããç°¡åã«æ¶ããã³ã³ãã€ã©ãŒã¯ããããã³ãŒãã«ãã€ã³ã©ã€ã³åãããããµã³ãã«ããé 眮ãããã®ããªããªããŸãã
dtraceã®è¬çœªè ã§ãããã¬ã³ãã³ã°ã¬ãã°ã¯ãããããã¹ãŠã®çæ°ã®æ¥ã ã®æåž«ããã³æå°è ã«ãªããŸããã
å Žåã«ãã£ãŠã¯ããã®[dtrace]ã¯åªããããŒã«ã§ã¯ãªããå¯äžã®ããŒã«ã§ãã圌ã¯å®£èšããã
å¿ èŠãªãã®ã«äŒŒããã®ããããŸããããã¬ã³ãã³ã¯ã³ã¡ã³ããæ®ããŸããã
FreeBSDïŒDTraceã¯Solarisãšåæ§ã«äœ¿çšã§ããŸãã æ©äŒãããã°ãäŸãå ±æããŸããä»æ¥ãŸã§ãæ®å¿µãªããšã«ããã®äºä»¶ã¯åœŒã«ã¯èµ·ãããªãã£ãã ããããå®éã«ã¯ããã¹ãŠã¯Solarisãšåãã§ãããsbrkã®ä»£ããã«mmap \ munmapã®ã¿ãåŒã³åºãããŸãã
ç§ã¯Dèšèªã®ç¥æµãæãäžããªããã°ãªããŸããã§ããããããšãã°ãç¹ã«éçŽãããå€ã®ãã£ã¹ããªã©ãå€ãã®åéãæã¡è² ããããšã¯ã§ããŸããã§ããã
æåã«ã ããããå°ãããçŽããã¹ã¯ãªãããè©Šãããšã«ããŸãã ïŒ
ã³ãŒã
#!/usr/sbin/dtrace -s /*#pragma D option quiet*/ /*#pragma D option cleanrate=5000hz*/ pid$1::mmap:entry { self->addr = arg0; self->size = arg1; } pid$1::mmap:return /self->size/ { addresses_mmap[arg1] = 1; printf("<__%i,%Y,mmap(0x%lx,%d)->0x%lx\n", i++, walltimestamp, self->addr, self->size, arg1); /*ustack(2);*/ printf("__>\n\n"); @mem_mmap[arg1] = sum(1); self->size=0; } pid$1::munmap:entry /addresses_mmap[arg0]/ { @mem_mmap[arg0] = sum(-1); printf("<__%i,%Y,munmap(0x%lx,%d)__>\n", i++, walltimestamp, arg0, arg1); } pid$1::malloc:entry { self->size = arg0; } pid$1::malloc:return /self->size > 0/ { addresses_malloc[arg1] = 1; /* printf("<__%i,%Y,malloc(%d)->0x%lx\n", i++, walltimestamp, self->size, arg1); ustack(2); printf("__>\n\n"); */ @mem_malloc[arg1] = sum(1); self->size=0; } pid$1::free:entry /addresses_malloc[arg0]/ { @mem_malloc[arg0] = sum(-1); /*printf("<__%i,%Y,free(0x%lx)__>\n", i++, walltimestamp, arg0);*/ } END { printf("== REPORT ==\n\n"); printf("== MMAP ==\n\n"); printa("0x%x => %@u\n",@mem_mmap); printf("== MALLOC ==\n\n"); printa("0x%x => %@u\n",@mem_malloc); }
ããã¯éåžžã«åçŽã«èŠããŸãïŒmalloc \ freeãžã®ãã¹ãŠã®åŒã³åºããšãããã®åŒã³åºãã®å Žæãä¿åããŸãã mallocã§ã¯-ã¢ãã¬ã¹ã®ã«ãŠã³ã¿ãŒãå¢ãããfreeã§ã¯-æžãããŸãã 次ã«ãåä¿¡ãããã°ã調ã¹ãŠããªãŒã¯ïŒcounters> 0ã®ã¢ãã¬ã¹ïŒãèŠã€ããŸãã å šäœçãªåé¡ã¯ã1ç§ãããçŽ150K mallocsã§ãustackïŒïŒé¢æ°ïŒã¹ã¿ãã¯ãã€ãŸãåŒã³åºãã®å Žæãä¿åããïŒãããã»ã¹å šäœããã®éã¿ã§æåéãåãå§ããããšã§ãïŒvalgrindã®å Žåãšåæ§ïŒã åºåããã¹ã¿ãã¯ãåé€ããã¢ãã¬ã¹ãšã«ãŠã³ã¿ãŒãåçŽã«åéããããšããŸãããããã®çµæãäœããã®çç±ã§ãå€ãã®ã«ãŠã³ã¿ãŒããã€ãã¹ã«æ·±ããªã£ãŠããŸãïŒå®éã«ã¯ãå°ç¡ãã«ãããããŒããšããã«ãªãªãŒã¹ã§ããïŒïŒããããŠãå®éã«ã¯æ£ã®ã«ãŠã³ã¿ãŒå€ãæã€ã¢ãã¬ã¹ã¯ãããŸããã§ãã...åæã«ãdtrace次ã®ãããªåãåºããšã©ãŒïŒ
dtrace: 3507 dynamic variable drops with non-empty dirty list dtrace: 2133 dynamic variable drops dtrace: 120 dynamic variable drops with non-empty dirty list dtrace: 993 dynamic variable drops dtrace: 176 dynamic variable drops with non-empty dirty list dtrace: 1617 dynamic variable drops dtrace: 539 dynamic variable drops with non-empty dirty list dtrace: 10252 dynamic variable drops dtrace: 3830 dynamic variable drops with non-empty dirty list dtrace: 17048 dynamic variable drops dtrace: 39483 dynamic variable drops dtrace: 1121 dynamic variable drops with non-empty dirty list dtrace: 35067 dynamic variable drops dtrace: 32592 dynamic variable drops dtrace: 10081 dynamic variable drops with non-empty dirty list
ãŸããã¹ã¿ãã¯äžã®å£ããã¢ãã¬ã¹ã«é¢ããã¡ãã»ãŒãžãã¡ãã€ãããšããããããŸããã
ããã¯ããã¹ãŠã®ã€ãã³ããæ£ããåŠçããæéããªãã£ããããã«ãŠã³ã¿ãŒããã€ãã¹ã«ãªã£ãŠããããšã瀺åããŠããŸã...ãŸãã¯ãããã¯ããŒã/ã¹ã¿ãã¯ã®åé¡ã§ãã ããã«ãmallocsã®1.5åã®ç¡æã®åŒã³åºãããããŸããïŒã€ãŸããããã«ãªãªãŒã¹ã§ããïŒïŒã
ç§ã¯dtraceã¡ãŒãªã³ã°ãªã¹ãã«å©ããæ±ããããšã«ããŸããã
ã¢ãŒã«ã€ãããå€æãããšããã€ãŠå¿ããã£ããã¥ãŒã¹ã¬ã¿ãŒã¯ã€ããææãè¿ããŠããŸããã é©ããããšã«ãæåã®æ°åã§çããåºãŸãããã2人ã®åå è ããåå¿ããŸããã§ããã ä¿åããã¹ã¿ãã¯ãã¬ãŒã ã®æ°ãæå®ããåŒæ°ustackïŒnframesïŒã䜿çšããããšããå§ãããŸãã ããã¯å©ãã«ãªãããustackïŒ1ïŒã§ããããã»ã¹å šäœã匷å¶çµäºããŸããã ãŸããlibumemã䜿çšããããã«ã¢ããã€ã¹ãããŸããïŒãããããSolarisã§ã¯ããŒã«ãããšæãããŸãïŒã
ãã®åŸã次ã®ãããªéäœç³»çãªè©Šã¿ãè¡ãããŸãããmallocsã«ãã£ãŠå²ãåœãŠããããµã€ãºã«é¢ããçµ±èšãåéããã»ã³ãµãŒã®é »åºŠãäœããã®æ¹æ³ã§æžããããã«ãç¹å®ã®ãµã€ãºã®ã¿ããã£ã«ã¿ãŒã§é€å€ããããšããŸããã ç¡é§ã«ãustackïŒïŒã¯ã1ç§ãããæ倧100ã³ãŒã«ãªã©ãæãæå°ã®è² è·åãã«èšèšãããŠããããã«æããããŸãã ãŸãã¯ããªã¯ãšã¹ãããšã«ã¹ã¿ãã¯ãã¹ãã³ãããã«ãçµæãç¡éã®å éšãããã¡ã«ä¿åããŠãæ£ããã¯ãã¯ã§ããããã«ããå¿ èŠããããŸãã ããããæ®å¿µãªãããããã«ã¯è³ããŸããã§ããã
ç§ã¯å察åŽããã¿ã¹ã¯ã«ã¢ãããŒãããããšããŸãã-ã³ãŒãå ã®ãã¹ãŠã®ãªããžã§ã¯ãã®ãã¶ã€ããšãã¹ãã©ã¯ã¿ã®åŒã³åºããã«ãŠã³ãããããã«ãå®éã«ã¹ã¿ãã¯ãä¿åããå¿ èŠã¯ãããŸããã ãããçµæãçã¿åºããŸããã§ããã ççŸã¯æããã«ãªããŸããã§ãããããã¡ãããã³ãŒãå ã®ãã¹ãŠã®ãªããžã§ã¯ããããã¹ããããã®ã¯é¢åã§ãçããããã®ã«éå®ããŸããã
ããã³ã°ã«ã«ã€ããŠ
é¢æ°ã®åŒã³åºãã§ã»ã³ãµãŒãã€ã³ã¹ããŒã«ããã«ã¯ããããã ååã®ããã³ã°ã«ã ããã§ã¯ãããããæªã§ãããã®ããã«ãFreeBSDã¯å¥ã®é©ããæããããŸãã ã ã·ã³ãã«ïŒ
ãŠãŒãã£ãªãã£ãã¯ã©ãã·ã¥ãããŸãã ãã®ãã°ã¯11.1ã§ã¯ãŸã ä¿®æ£ãããŠããŸããã ããŒãžã§ã³11.0ã®ãµãŒããŒã®1ã€ã§ããã³ã°ã«åŠçãå®è¡ããå¿ èŠããããŸããã
echo _ZZN7simlib318SIMLIB_create_nameEPKczE1s | /usr/bin/c++filt
ãŠãŒãã£ãªãã£ãã¯ã©ãã·ã¥ãããŸãã ãã®ãã°ã¯11.1ã§ã¯ãŸã ä¿®æ£ãããŠããŸããã ããŒãžã§ã³11.0ã®ãµãŒããŒã®1ã€ã§ããã³ã°ã«åŠçãå®è¡ããå¿ èŠããããŸããã
ãã¹ãŠã颚è»ãšã®æŠãã«äŒŒå§ããŸããã
äžè¬çã«ãdtraceã®åºåã¯ããªãŒã¯ã§ã¯ãªããç ŽæããããŒã/ããã«ãªãªãŒã¹ã§ãããšäž»åŒµããŸããã ãããæ¬åœãªããç¹å¥ãªãã®ãªãã§ããã¹ãŠãéåžžã«æªãã§ãã ã©ã€ãã©ãªã¯ååã§ã¯ãããŸããã
ããããããã¯ãã¹ãŠå®å šãªãã³ã»ã³ã¹ã§ããããšãå€æããæ°æ¥éã ãæšçããåŽã«éããŸããã
jemalloc
FreeBSDèªäœãéåžžã«ã¯ãŒã«ã§é«åºŠãªã¡ã¢ãªãããŒãžã£ã䜿çšããŠããããšã¯æ³šç®ã«å€ããŸãã ããããç§ã¯ãããå®å šã«å¿ããŠããŸãã... èšå®ãšãªãã·ã§ã³ã®æ°ã«ãã£ãŠãä»ã®ãã¹ãŠã®ã©ãããŒã©ã€ãã©ãªã¯è¿ãã«ãããããŸããã§ããã ããã¯ãããããåããåŸãç§ã¯ãªãã·ã§ã³ã§ã¢ããªã±ãŒã·ã§ã³ãèµ·åããããšãã§ããŸããïŒ
setenv MALLOC_CONF utrace:true
次ã«ã ããããã®æ瀺ã«åŸã£ãŠããã¹ãŠã®ã¡ã¢ãªæäœã®ãã°ãåéïŒktraceïŒããã³çæïŒkdumpïŒããŸãã ãã®äžã«ã¯èšäºã®ã¹ã¯ãªããã§ã¯æ©èœããªãã£ãreallocããããã³ã¡ã³ãããã®ã¹ã¯ãªããã®2çªç®ã®ããŒãžã§ã³ ïŒreallocãæ£ããåŠçããïŒã¯ååšããªãããŒãžã«ã€ãªãããŸããïŒãããŠWayback Machineã§ãäœãèŠã€ãããŸããã§ããïŒã reallocsã®ãµããŒããè¿œå ããå¿ èŠããããŸããããå®éã«ã¯ããªãŒã¯ãçºçããŠããå¯èœæ§ã®ããå Žæã«å€§éã®ãã€ã³ã¿ãæäŸããã ãã§ãã©ãã«èª°ãå²ãåœãŠãããŠãããã«ã€ããŠã®ãã³ãã¯ãããŸããã§ããã
è¿ãã®ãã¬ãŒã¹åºåããã£ãšèª¿ã¹ãããšã§ããœãŒã¹ã®ãªãŒã¯ã®å Žæã«ã€ããŠããå°ãç解ã§ãããããããŸãã:-)-èšäºã®èè ãåè«ãèšã£ãã
ä»ã®jemallocãªãã·ã§ã³ãæ©èœããããã«ã¯ïŒããšãã°ãäºé解æŸã®æ€åºãé åããã®è±åºãã¡ã¢ãªãžã®ã¢ã¯ã»ã¹ã«é¢ããæ¡åŒµçµ±èšãªã©ïŒãã³ã¢å šäœãäœåã«å¿ èŠã§ãã ãªãã·ã§ã³ããŸãã¯çŸåšã®OSã¢ã»ã³ããªïŒäººéããç解ã§ãããã®ïŒã ããããããã¯æ€çŽ¢ã®6æ¥ç®ã§ãããjemallocã®ãããªãæãäžãã¯äžæãããããšã«ãªããŸãããããã®ãããã¯ã¯éåžžã«èå³æ·±ããã®ã§ãããç§ã¯ããã«æ»ããããšæããŸãïŒåæ§ã®ç¶æ³ã§ã¯ãªãããšãæã¿ãŸãïŒã
æçµçã«ãªãŒã¯ã®çºèŠã«åœ¹ç«ã£ããã®
åã¢ãžã¥ãŒã«ãšã¢ããªã±ãŒã·ã§ã³ãµãã·ã¹ãã ãããã³ãããã®çµã¿åãããé çªã«ç¡å¹ã«ããŸãã ç§ã¯æåãããã®æ¹æ³ã§è¡ã£ãŠããŸããããåé¡ã¯1æ¥ä»¥å ã«è§£æ±ºããŸããããèŠéããŠã¯ãªããªãæ°ããèå³æ·±ããã®ãããã€ãããŸããïŒ
ãã¹ãŠã®æ²ããç©èªã«ã¯ãè¯å®çãªåŽé¢ããªããã°ãªããŸããã
ããžãã£ãã®ããŒã
- éãæ€çŽ¢ããéçšã§ãå€ãã®å°ããªïŒãããŠããã§ã¯ãªãïŒãã°ãäž»ã«ã¡ã¢ãªå²ãåœãŠãä¿®æ£ãããŸããã
- ARC ZFSãåé€ããŸããïŒãã£ã¹ã¯ã«äœãæžã蟌ãŸããã¡ã¢ãªã®ã¿ãæ¶è²»ãã éåžžã«éèŠãª HTTPãµãŒããŒã§å¿ èŠãªçç±ïŒã
- C ++ã®äžçããã®ä»®æ³ãã¹ãã©ã¯ã¿ããã³ãã®ä»ã®ããªãããŒãªããŒã¹ã«é¢ããèšäºãç 究ãããŸããã
- Dã§å€æ°ã®ã¹ã¯ãªããã䜿çšããŠäœæããããæ°ãåããªãã»ã©ã®ãã¥ãŒããªã¢ã«ãïŒdtraceã«ãã£ãŠïŒè¡šç€ºãããŠããŸãã
- malloc \ freeã®ããŸããŸãªå®è£ ã«æ²¡é ããã¢ãªãŒããšã¹ã©ãã®æŠå¿µã«ç²ŸéããŠããã
- ãããžã§ã¯ãã§äœ¿çšãããŠãããã¹ãŠã®ãµãŒãããŒãã£ã©ã€ãã©ãªã®ã¡ã¢ãªã«é¢é£ããæ¢ç¥ã®ãã°ããã¹ãŠèª¿æ»ããŸããã
- Solaris OSãååšããæåŸã®å¹Žã«å±éããæŽå²ãšãã©ããåŠã³ãŸããã
- GDBã¹ãã«ã®ã¢ããã°ã¬ãŒã/埩å ã
å®å šãªçµ¶æã®ç¬éã«é¥ãã¹ãã§ãªãçæ°
- ãã¹ãŠãéã£ãŠLinuxã«ç§»è¡ããŸãã ãã®èãã¯ãå®å šãªGoogle ChromeãSkypeããŸãã¯ã¯ãŒã«ãªèªäœã·ã¹ãã ãŠãŒãã£ãªãã£ã®äžè¶³ãåå ã§ãFreeBSDã®ç±å¿ãªãµããŒã¿ãŒãç¹°ãè¿ã蚪ããŠãããšæããŸã ã ãããã¯ã«é¥ããªãã§ãã ãããé ããæ©ãããã¹ãŠã®åé¡ã解決ããããæ°ã«å ¥ãã®OSã«åã³æ»ããŸãã
- hex-viewerãã¹ã¯ããããŒãã£ã·ã§ã³ã«ã¯ããŒã«ããããšããŸãïŒã¡ã¢ãªããããã·ã¥ãããŠãããã®ã確èªãããªãŒã¯ã®å Žæãæšæž¬ããããšããŸãïŒã
- ã¢ããªã±ãŒã·ã§ã³ãããã¹ãŠã®ãã«ãã¹ã¬ãããåé€ããŠãã»ãšãã©ã®è³éãããé«ã皌ãããããã«ããŸããã
- ããã®ããªãŒã©ã³ãµãŒãéã£ãŠããã¹ãŠããã°ããä¿®æ£ããŸãã
çµè«
- ãªãªãŒã¹ã®ãã¹ãŠã®å€æŽã培åºçã«èšé²ããŸãã ã»ãšãã©ãã¹ãŠã®å ŽåããªãŒã¯ã¯ææ°ã®ã³ãããã®1ã€ã«é¢é£ä»ããããŸãã ããªããéåžžã«äžéãªããæåŸã®æ°ã¶æã«ããã£ãŠã³ãããããŠãã ããã ãã¹ãŠã®å€æŽãäžåºŠã«1ã€ãã€ããŒã«ããã¯ããçµã¿åãããŠããã¹ãŠã®å€æŽãæ°åå確èªããŠãã ããã ããããªééã-ãããŠãããªãã¯ééã£ãæ¹åã«æ°æ¥é/æ°é±éé£ãå»ãããŸãã
- æéã貎éãªå Žå-ç¹ã«é«è² è·ã®ãããžã§ã¯ãã§ã¯ãæ±çšãªãŒã¯æ€åºããŒã«ã䜿çšããªãã§ãã ããã
- dtraceã®å·§åŠãã¯ååã§ã¯ãªãããã¹ãŠã®ãªã¯ãšã¹ãã®ããäžéšããåŠçã§ããŸããã§ããã ãã ããããã§ã®äºåŸåŠçã§ã¯ã誀æ€åºãªã©ãæ£ããé€å€ããå¿ èŠããããŸãã æ®å¿µãªããããã®ã¢ãããŒããç¿åŸããã®ã«ååãªæéãšãšãã«ã®ãŒããããŸããã§ããã
ãã®ç©èªã®çµããã¯å€§ããªéã ãšæããŸãã æŒããæ€çŽ¢å šäœã§å®å®ããŠåçŸãããããšã¯å¹žéã§ããã ãªãªãŒã¹ãµãŒããŒã®1ã€ã§åæ§ç¯ãããã¢ããªã±ãŒã·ã§ã³ãããŠãŒã¶ãŒã«å€§ããªæ害ãäžããããšãªãå¿ èŠãªåæ°ã ãåèµ·åããæ©äŒããã£ãããšã ãã°ããªãªãŒã¹ãããŠããåé¡ãçºçãããŸã§ã«ãåé¡ãèŠã€ããå¯èœæ§ããŒãã«ãªããšå幎ã§ã¯ãªãããããæ°æ¥ããããããŸããã§ãã...
質çå¿ç
çµå±ã®ãšãããæŒãã®åå ã¯äœã ã£ãã®ã§ããããïŒ
åè¿°ã®ããã«ããµãŒããŒã¯äžäœãµãŒããŒã®å¿çéã§ãªãŒã¯ã·ã§ã³ãéå¬ããŸãã åçãæ倧å€ãè¶ ããå Žåããããã¯ãã¯ãªããã³ã°ããããŸãïŒäžäœNåã®çµæãååŸãããŸãïŒã
äžéšã¯std :: listã«æ ŒçŽãããŸããããã§ãèŠçŽ ã¯bidãªããžã§ã¯ããžã®ãã€ã³ã¿ãŒã§ãã ã³ãããã®1ã€ã§ãããããããªãã³ã°ããããã®ãã°ãããã³ãŒãlist.resizeïŒmax_resultsïŒãå°å ¥ãããŸããã ãæ³åã®ãšãããlist.resizeã¯ãã€ã³ã¿ãŒèŠçŽ ã§deleteãåŒã³åºããŸããã resizeãåŒã³åºãåã«ããã³ãã«ã䜿çšããŠãã¹ãŠã®äœåãªãã€ã³ã¿ãŒã®ã¡ã¢ãªã解æŸããå¿ èŠããããŸãã
ãã°ããŒãžã§ã³ã®ãªãªãŒã¹åŸããªããããªã«é·ãéãªãŒã¯ãæããããªãã£ãã®ã§ããïŒ
ãµãŒããŒã®å¿çã¯åžžã«äžäœNã®çµæã«ãªããäœãåãæšãŠãããŸããã§ããã ããæç¹ã§ç¹å®ã®ãŠãŒã¶ãŒãããå€ãã®åçãæã¡ãããããšã®å¹²æžãããããšããããšã§ãã
以åã®ããŒãžã§ã³ã«ããŒã«ããã¯ããªãã£ããããåé¡ãããã«ç¹å®ã§ããªãã£ãã®ã¯ãªãã§ããïŒ
ããã§æŒããããã®ã¯ãã¥ãŒãã³ãã¡ã¯ã¿ãŒã§ãã å®éã«ã¯ãã¢ããªã±ãŒã·ã§ã³ã¯éå§æã«ã¡ã¢ãªãç©æ¥µçã«æ¶è²»ãå§ããæ°æé匷床ãäœäžãããŸãã æãããæ€çŽ¢ã®æäžã«ãããã¯ç¶ç¶çãªãªãŒã¯ãšããŠèªèãããåé¡ç®æãç¹å®ã§ããŸããã§ããã
ãµãŒããŒã®ç¹å®ã®éšåã§ã®ã¿åé¡ãçºçããã®ã¯ãªãã§ããïŒ
ãã®ãµãŒããŒã°ã«ãŒãã§ãµãŒãã¹ãæäŸããã¢ã«ãŠã³ãã®ããã ãªãŒã¯ã·ã§ã³ã«åå ããäžéšã®äŒæ¥ã¯ãäžäœã«åãŸããªã倧éã®å ¥æãè¿ãå§ããŸããã ä»ã®ãµãŒããŒãæäŸããä»ã®ã¢ã«ãŠã³ãã«ã€ããŠã¯ããã¹ãŠã®å¿çãããããªãŒã¯ã·ã§ã³ã«å«ãŸããŠããããæ倧å€ãè¶ ããããšã¯ã»ãšãã©ãããŸããã§ããã ãªã¹ããµã€ãºã
ç¹ã«Javaã§ããªãŒã¯ã«å¯ŸåŠããã¹ããŒãªãŒãæããŠãã ããã ãNåããšã«ã¢ããªã±ãŒã·ã§ã³ãåèµ·åãããæ¹æ³ã¯ãæ®éçãªãœãªã¥ãŒã·ã§ã³ãšããŠéåžžã«äººæ°ããããŸããïŒ Linuxããtcmalloc and coãã§ã¯ãšãŠãæ²ããã®ã§ããã
UPD1 ïŒãã®ããã¹ãã®æºåäžã«ã clangã䜿çšããŠãªãŒã¯ãæ€åºã§ããå¯èœæ§ã«å¶ç¶åºããããŸããã
æåŸãŸã§èªãã§ãããã¿ããªã«æè¬ããŸãïŒ