From owner-freebsd-hackers@FreeBSD.ORG Tue Mar 10 16:07:21 2015 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id BB60344C for ; Tue, 10 Mar 2015 16:07:21 +0000 (UTC) Received: from mail-wi0-x22b.google.com (mail-wi0-x22b.google.com [IPv6:2a00:1450:400c:c05::22b]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 48F6B3A7 for ; Tue, 10 Mar 2015 16:07:21 +0000 (UTC) Received: by wibbs8 with SMTP id bs8so31542706wib.0 for ; Tue, 10 Mar 2015 09:07:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=Ip/B4kUG/+xTOr2qQsLtaYNTgYTlxVG+XlYFb5MEmvQ=; b=bgp5MuXIxC49w+iuBRbhIpGK7Xswp0KFwvEIFZVTMvpzkmCgSBVki8BO5+iSHXjG1y n/klRprg4OUkl4KUUlMRXoj63iE/Y52RjBpepvwLIHNQU63QlDZQDs2Fe9Y/+xGTGm8a 2/BTXKKCiGOKDfsBuGuGP4lvwOdT+dcmiF1ZHw7+6DNzxDLvrNJkkgECKYxO5pBsMNs3 P2bfxMn174kx4dRawovMa7seG8iIk1l9UFeuZw2UcdD/yR9jFWYyFZYioNXuV+wAXhig bUJH/ishaMxRRAMQsOQWjkfmAr+kmlA9iSb6H3WnhKStdhLsn6wlKgBs+lcMXKCIC8ou b4Zw== MIME-Version: 1.0 X-Received: by 10.180.78.136 with SMTP id b8mr72939318wix.6.1426003639746; Tue, 10 Mar 2015 09:07:19 -0700 (PDT) Sender: asomers@gmail.com Received: by 10.194.17.129 with HTTP; Tue, 10 Mar 2015 09:07:19 -0700 (PDT) In-Reply-To: References: <9F2E1411-B517-4BC8-AF61-BB15EE35083C@me.com> <54FF1343.1020705@gmx.de> Date: Tue, 10 Mar 2015 10:07:19 -0600 X-Google-Sender-Auth: 0nHKXgNie-nY0mupEasLlTG-CSw Message-ID: Subject: Re: detecting hyperthreading From: Alan Somers To: Freddie Cash Content-Type: text/plain; charset=UTF-8 Cc: "freebsd-hackers@freebsd.org" , "lokadamus@gmx.de" , "Pokala, Ravi" , Rui Paulo X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 Mar 2015 16:07:21 -0000 On Tue, Mar 10, 2015 at 10:02 AM, Freddie Cash wrote: > On Tue, Mar 10, 2015 at 8:56 AM, Pokala, Ravi wrote: > >> -----Original Message----- >> From: "lokadamus@gmx.de" >> Date: 2015-03-10, Tuesday at 08:52 >> To: Ravi Pokala , Rui Paulo >> Cc: "freebsd-hackers@freebsd.org" >> Subject: Re: detecting hyperthreading >> >> >Have you look at dmesg? >> >My system is a P4 with HTT. >> >dmesg |more >> [...] >> >CPU: Intel(R) Pentium(R) 4 CPU 3.00GHz (3000.00-MHz 686-class CPU) >> > Origin = "GenuineIntel" Id = 0xf29 Family = 0xf Model = 0x2 >> >Stepping = 9 >> > >> >Features=0xbfebfbff> >CA,CMOV,PAT,PSE36,CLFLUSH,DTS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE> >> > Features2=0x4400 >> >> Of course. :-) >> >> But there are two problems: >> >> (1) That just tells me HTT is supported by the CPU, not that if kernel is >> using it. >> (2) It's difficult to parse. >> >> Of the two, (1) is the bigger concern for my use-case. >> > > 6 lines or so below the Features line shows the kernel loading "cpu0 > (BSP)", and then "cpu1 (AP/HT)". > > Compare that to a system without HTT, where any extra cpus only show "(AP)". > > It's not perfect, but one could grep through /var/run/dmesg.boot looking > for "cpu" lines and checking for "(AP)" or "(AP/HT)". > > -- > Freddie Cash > fjwcash@gmail.com I always look at "sysctl kern.sched.topology_spec" to tell if hyperthreading is enabled. It's overkill, but it works. Here's some Ruby code that can parse it: require 'rexml/document' # Parses the output of "sysctl kern.sched.topology_spec and returns a triple # of [number of sockets, cores/socket, threads/core] def _parse_topo(topology) cpu_xpath = "groups/group/children/group/cpu" xmldoc = REXML::Document.new(topology) _punt_topo(topology) unless xmldoc.get_elements("groups/group").count == 1 sockets = xmldoc.get_elements("groups/group/children/group").count all_cores_per_socket = Set.new([]) all_threads_per_core = Set.new([]) xmldoc.get_elements("groups/group/children/group").each do |group2| cores = 0 children = group2.get_elements("children/group") if children.empty? # No hyperthreading cpu_entities = group2.get_elements("cpu") _punt_topo(topology) unless cpu_entities.count == 1 core_count = cpu_entities.first.attribute("count").value cores += core_count.to_i else children.each do |group3| if group3.get_elements("flags/flag[@name='SMT']").empty? && \ group3.get_elements("flags/flag[@name='HTT']").empty? && \ group3.get_elements("flags/flag[@name='THREAD']").empty? else # This cpu group represents a single hyperthreaded core cores += 1 cpu_entities = group3.get_elements("cpu") _punt_topo(topology) unless cpu_entities.count == 1 core_count = cpu_entities.first.attribute("count").value all_threads_per_core.add(core_count.to_i) end end end all_cores_per_socket.add cores end if all_cores_per_socket.size > 1 cores_per_socket = "mixed" else cores_per_socket = all_cores_per_socket.first.to_i end if all_threads_per_core.size > 1 threads_per_core = "mixed" elsif all_threads_per_core.size == 0 # No SMT sections means no hyperthreading threads_per_core = 1 else threads_per_core = all_threads_per_core.first.to_i end [sockets, cores_per_socket, threads_per_core] end # Helper method for _parse_topo def _punt_topo(topo) STDERR.puts topology raise IOError.new "Output of kern.sched.topology is not understood" end -Alan