From owner-freebsd-questions@FreeBSD.ORG Thu Apr 1 22:19:44 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 40EAD16A4CE for ; Thu, 1 Apr 2004 22:19:44 -0800 (PST) Received: from phxby.engr.usu.edu (phxby.engr.usu.edu [129.123.21.101]) by mx1.FreeBSD.org (Postfix) with ESMTP id 1001943D45 for ; Thu, 1 Apr 2004 22:19:44 -0800 (PST) (envelope-from irwanhadi@phxby.com) Received: by phxby.engr.usu.edu (Postfix, from userid 1001) id AA00745102; Thu, 1 Apr 2004 23:19:43 -0700 (MST) Date: Thu, 1 Apr 2004 23:19:43 -0700 From: Irwan Hadi To: freebsd-questions@freebsd.org Message-ID: <20040402061943.GA54789@phxby.com> Mail-Followup-To: freebsd-questions@freebsd.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4.2i Subject: High context switches on FreeBSD X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 02 Apr 2004 06:19:44 -0000 I'm a simple iterative web server for a small project. This web server uses regular read(2) and write(2) to both read file from the drive and write it to the socket. When I tested this web server on my FreeBSD 5.2.1 box, on a P IV 3 Ghz Processor and 1 GB memory, I'm getting an enormous number of context switches, as you can see below: # vmstat 1 procs memory page disks faults cpu r b w avm fre flt re pi po fr sr da0 da1 in sy cs us sy id 0 0 0 119644 736332 2 0 0 0 2 0 0 0 297 0 212 0 0 100 0 0 0 119644 736332 4 0 0 0 4 0 0 0 294 0 200 0 0 100 0 0 0 119644 736332 0 0 0 0 0 0 1 0 298 0 209 0 0 100 0 0 0 119644 736348 0 0 0 0 4 0 4 0 308 0 303 0 1 99 0 0 0 119644 736348 0 0 0 0 0 0 0 0 301 0 221 0 0 100 0 0 0 119644 736348 0 0 0 0 0 0 0 0 300 0 215 0 0 100 1 0 0 121336 736092 82 0 0 0 19 0 0 0 9203 0 50953 9 18 73 1 0 0 121336 736040 13 0 0 0 0 0 0 0 18315 0 130743 14 46 40 0 0 2 121336 735984 14 0 0 0 0 0 0 0 18514 0 128537 18 41 41 1 0 0 121336 735928 14 0 0 0 0 0 0 0 20378 0 103812 15 40 45 0 0 2 121336 735868 15 0 0 0 0 0 2 0 20029 0 107131 14 40 45 0 0 2 121336 735804 16 0 0 0 0 0 4 0 22032 0 79700 17 33 50 1 0 0 121336 735744 15 0 0 0 0 0 0 0 21645 0 87448 16 35 49 0 0 1 121336 735684 15 0 0 0 0 0 0 0 20195 0 105555 17 39 44 0 0 2 121336 735620 16 0 0 0 0 0 0 0 22129 0 81349 19 31 50 0 0 1 121336 735564 14 0 0 0 0 0 5 0 20134 0 106842 19 36 45 0 0 0 118992 736576 12 0 0 0 265 0 0 0 8283 0 58152 7 18 75 0 0 0 118992 736576 0 0 0 0 0 0 0 0 295 0 197 0 1 99 0 0 0 118992 736576 0 0 0 0 0 0 0 0 297 0 206 0 0 100 0 0 0 118992 736576 0 0 0 0 0 0 0 0 296 0 201 0 0 100 0 0 0 118992 736576 0 0 0 0 0 0 0 0 301 0 208 0 0 100 0 0 0 118992 736576 0 0 0 0 0 0 0 0 294 0 198 0 0 100 0 0 0 118992 736576 0 0 0 0 0 0 0 0 301 0 210 0 1 99 ^C # When I run the same code on a Linux box, I'm only getting at most 33000 context per second, thus it gives a better performance. My question now is, is there a way I can trace the source of these context switches, network, or disk? I'm thinking it may be disk, though it is also possible that it is caused because of network. Also, I tried testing the web server by using ramdisk, and the result is still the same. The context switches stays around 100,000 at peak. I'm hoping by reducing the number of context switches, I can increase my web server performance. Oh by the way, again this web server is my own, and I just use read(2), and write(2) to read and write to the socket, and to read the file from the disk. Thanks