{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/3285","repository_url":"https://api.github.com/repos/dotnet/coreclr","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/3285/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/3285/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/3285/events","html_url":"https://github.com/dotnet/coreclr/pull/3285","id":135159290,"number":3285,"title":"Fix test issue on NetBSD: Walk-around getrusage(2) implementation nits","user":{"login":"krytarowski","id":6665730,"avatar_url":"https://avatars.githubusercontent.com/u/6665730?v=3","gravatar_id":"","url":"https://api.github.com/users/krytarowski","html_url":"https://github.com/krytarowski","followers_url":"https://api.github.com/users/krytarowski/followers","following_url":"https://api.github.com/users/krytarowski/following{/other_user}","gists_url":"https://api.github.com/users/krytarowski/gists{/gist_id}","starred_url":"https://api.github.com/users/krytarowski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/krytarowski/subscriptions","organizations_url":"https://api.github.com/users/krytarowski/orgs","repos_url":"https://api.github.com/users/krytarowski/repos","events_url":"https://api.github.com/users/krytarowski/events{/privacy}","received_events_url":"https://api.github.com/users/krytarowski/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/coreclr/labels/cla-already-signed","name":"cla-already-signed","color":"009800"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-02-21T04:45:39Z","updated_at":"2016-02-21T04:56:27Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/dotnet/coreclr/pulls/3285","html_url":"https://github.com/dotnet/coreclr/pull/3285","diff_url":"https://github.com/dotnet/coreclr/pull/3285.diff","patch_url":"https://github.com/dotnet/coreclr/pull/3285.patch"},"body":"The following code:\r\n\r\n```\r\nint main(int argc, char **argv)\r\n{\r\n    int i, j, k, total;\r\n    struct rusage resUsage1, resUsage2;\r\n\r\n    if (getrusage (RUSAGE_SELF, &resUsage1) == -1)\r\n        errx(1, \"getrusage\");\r\n\r\n    /* simulate some activity */\r\n    for( i=0; i<1000; i++ )\r\n    {\r\n        for( j=0; j<1000; j++ )\r\n        {\r\n            total = j * i;\r\n            for( k=0; k<1000; k++ )\r\n            {\r\n                total += k + i;\r\n            }\r\n        }\r\n    }\r\n\r\n    if (getrusage (RUSAGE_SELF, &resUsage2) == -1)\r\n        errx(1, \"getrusage\");\r\n\r\n    printf(\"kernel1 %ld.%06ld\\n\", resUsage1.ru_stime.tv_sec, resUsage1.ru_stime.tv_usec);\r\n    printf(\"kernel2 %ld.%06ld\\n\", resUsage2.ru_stime.tv_sec, resUsage2.ru_stime.tv_usec);\r\n\r\n    printf(\"user1 %ld.%06ld\\n\", resUsage1.ru_utime.tv_sec, resUsage1.ru_utime.tv_usec);\r\n    printf(\"user2 %ld.%06ld\\n\", resUsage2.ru_utime.tv_sec, resUsage2.ru_utime.tv_usec);\r\n\r\n    return 0;\r\n}\r\n```\r\n\r\nReturns the following values:\r\n\r\n```\r\n$ ./test\r\nkernel1 0.000262\r\nkernel2 0.000000\r\nuser1 0.000262\r\nuser2 2.279965\r\n```\r\n\r\nThe reason for it is as follows:\r\n\r\n```\r\n    The reason is that the scheduler keeps a precise total cpu time of\r\n    a process but the systime/usertime is only sampled by a much slower\r\n    clock. The ratio systime/usertime is then used to return an approximation\r\n    so that systime and usertime sum up to the total time(*).\r\n\r\n    As a result it is possible that usertime or systime go backwards.\r\n\r\n    FreeBSD has implemented a workaround by remembering reported\r\n    usertime/systime values for a process and clamping getrusage()\r\n    against these values. As a result the usertime/systime value\r\n    is usually too high but it is monotonic.\r\n\r\n    Another approach would be to round down the usertime/systime values,\r\n    by not approximating 'measurments' that do not exist. The result\r\n    would be that usertime/systime values that are too low and don't\r\n    sum up to the total CPU time but are again monotonic.\r\n\r\n    The approximation is done in kern_resource/calc().\r\n    (*) The sampled interrupt time is also included.\r\n```\r\n\r\n-- by Michael van Elst\r\n-- http://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=30115\r\n\r\nWalkaround it by doing kernel-work with subsequent `malloc`(3) and `free`(3) calls.\r\nNote that doing these calls for smaller chunks (it looks like a compiler/libc\r\ncan optimize it without going to the host kernel) or just 1000 times is not\r\nsufficient."},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/186745127","html_url":"https://github.com/dotnet/coreclr/pull/3285#issuecomment-186745127","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/3285","id":186745127,"user":{"login":"krytarowski","id":6665730,"avatar_url":"https://avatars.githubusercontent.com/u/6665730?v=3","gravatar_id":"","url":"https://api.github.com/users/krytarowski","html_url":"https://github.com/krytarowski","followers_url":"https://api.github.com/users/krytarowski/followers","following_url":"https://api.github.com/users/krytarowski/following{/other_user}","gists_url":"https://api.github.com/users/krytarowski/gists{/gist_id}","starred_url":"https://api.github.com/users/krytarowski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/krytarowski/subscriptions","organizations_url":"https://api.github.com/users/krytarowski/orgs","repos_url":"https://api.github.com/users/krytarowski/repos","events_url":"https://api.github.com/users/krytarowski/events{/privacy}","received_events_url":"https://api.github.com/users/krytarowski/received_events","type":"User","site_admin":false},"created_at":"2016-02-21T04:56:27Z","updated_at":"2016-02-21T04:56:27Z","body":"@jkotas PTAL"}}