From patchwork Wed Jan 18 19:56:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ronald Rojas X-Patchwork-Id: 9524653 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id F28EB60113 for ; Wed, 18 Jan 2017 19:59:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E2F9F28629 for ; Wed, 18 Jan 2017 19:59:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D395E2862A; Wed, 18 Jan 2017 19:59:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.6 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2B7AE2862D for ; Wed, 18 Jan 2017 19:59:38 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cTwMU-0004lW-P4; Wed, 18 Jan 2017 19:57:38 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cTwMU-0004kp-4U for xen-devel@lists.xen.org; Wed, 18 Jan 2017 19:57:38 +0000 Received: from [85.158.139.211] by server-10.bemta-5.messagelabs.com id 71/3B-04988-1B8CF785; Wed, 18 Jan 2017 19:57:37 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrGIsWRWlGSWpSXmKPExsVyMfTGId0NJ+o jDKbMMbVY8nExiwOjx9Hdv5kCGKNYM/OS8isSWDOW3FrFVLBSuWLxzntsDYzfpbsYuTiEBCYy SvT1LWcDcVgEXrJI3LnSzgLiSAj0s0oc/vCduYuRE8iJkbi89R0jhF0u8XpOC5gtJKAksfHmM naoUUwSO7a2soMk2AQ0JN5++ssKYosIZEjsn3uICcRmFlCTOPdqD5gtLBAmcaNtF1g9i4CqxL mfDWD1vALOEk9OvmOCWCYncfNcJ9gRnAIuEueeNDFDLHaW6H/0nn0Co8ACRoZVjOrFqUVlqUW 65npJRZnpGSW5iZk5uoYGpnq5qcXFiempOYlJxXrJ+bmbGIGhxQAEOxiPTXY+xCjJwaQkyjv5 aH2EEF9SfkplRmJxRnxRaU5q8SFGGQ4OJQne48eBcoJFqempFWmZOcAgh0lLcPAoifB+PgaU5 i0uSMwtzkyHSJ1iNObo6Tr9koljz67LL5mEWPLy81KlxHlngkwSACnNKM2DGwSLvkuMslLCvI xApwnxFKQW5WaWoMq/YhTnYFQS5l0GMoUnM68Ebt8roFOYgE6xUgY7pSQRISXVwKgnt6X8WNo 0nvc7b2y/0pzFMIk/plfCZHXyxjOqig3aR12rYk6+P6owY/HdHKEtU9MikvdqzmBmSPW7Va0g v0XoadoX7mQzja2HV9/oL+Odf/u+8A+hlnMPmuzu/3obUP+ifk5tzCIunZNinXffrni9g7mCP VQ3ybeFpanw9aR+Vim9mVdW9SmxFGckGmoxFxUnAgA+dfNDuQIAAA== X-Env-Sender: ronladred@gmail.com X-Msg-Ref: server-8.tower-206.messagelabs.com!1484769455!80480815!1 X-Originating-IP: [209.85.216.194] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.1.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 32922 invoked from network); 18 Jan 2017 19:57:36 -0000 Received: from mail-qt0-f194.google.com (HELO mail-qt0-f194.google.com) (209.85.216.194) by server-8.tower-206.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 18 Jan 2017 19:57:36 -0000 Received: by mail-qt0-f194.google.com with SMTP id n13so3659551qtc.0 for ; Wed, 18 Jan 2017 11:57:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=tKhmDZ0Vno4QmvQq/ZfVXvVeNL4gPKjkcXI9NXEdT3Y=; b=nCE49J9RVpidJMN/RNfc/pFVgs3jFwslw9xESqpLRMzoFstDo3Fl6F5QiwPkYCvnhY p9sNeDH2s3jzFtzolIsi0h57qNj2gxm440k8VNxsMvVo78o9c3sKbeCCLPZxWjvC10hV VKyHzjQ7rMaJxrHbaq/b5aLk6LYecz5MV2fUYV1X2P8STQacspRr7iLoYvWD/cavA/X+ m7y8m14FumD0f/N5yl+iDAex9f3eyjLWc4p+wUjw5SozWUEl5QNEgzmJYibNBAUY+xRc k0r6owWQbzG7GK34zlAJx+2G5MnIUXcuneF4DLjWjq+iyumhnliZPPR9eO4KoG/+iJ9e Lr/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=tKhmDZ0Vno4QmvQq/ZfVXvVeNL4gPKjkcXI9NXEdT3Y=; b=Zdiw4HIuxGqMhH4IL+no7pfQXYLhb9m0HBrHvYe3g84zFMImHXkJLGbAe0H5HJMrFz HjGz6rfnlJRczkzelKVsmPEQG/w9pX0vOP37UPeHZI7w+VIMfegO93D3/I6d24KSoSq1 WZjGlTYg/Psb+oM5OeRlLgxB6grZE3ILNYtkXN31yzhKx7A3sXBRDebI0op2pu/Zg0dQ S7uMHFDauS84uClpV0vlmUdPalt9QDzuq+Z3vDzyv1U3ziElkwuVIEVvBvJvhC6NdaNm pBgOLuvMTUWN1fGY7eI6N562K/lYwPfBUJqYd7CZOecgy31pdI/MsCNpfkV/pwZ8gWOD lbTw== X-Gm-Message-State: AIkVDXImHh8zF91WjhZ54OYjzUO5D2cUCgxZUWFYXkoHaLAxW2o46xiGadHVv1We2S49Ew== X-Received: by 10.200.40.45 with SMTP id 42mr4312696qtq.89.1484769455434; Wed, 18 Jan 2017 11:57:35 -0800 (PST) Received: from fedora (pool-173-68-188-67.nycmny.fios.verizon.net. [173.68.188.67]) by smtp.gmail.com with ESMTPSA id e3sm1089965qtg.7.2017.01.18.11.57.34 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 18 Jan 2017 11:57:34 -0800 (PST) From: Ronald Rojas To: xen-devel@lists.xen.org, george.dunlap@citrix.com, ian.jackson@eu.citrix.com, wei.liu2@citrix.com Date: Wed, 18 Jan 2017 14:56:43 -0500 Message-Id: <1484769406-17416-5-git-send-email-ronladred@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1484769406-17416-1-git-send-email-ronladred@gmail.com> References: <1484769406-17416-1-git-send-email-ronladred@gmail.com> Cc: Ronald Rojas Subject: [Xen-devel] [PATCH RFC 5/8] golang/xenlight: Implement libxl_bitmap and helper operations X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Implement Bitmap type, along with helper functions. The Bitmap type is implemented interllay in a way which makes it easy to copy into and out of the C libxl_bitmap type. Signed-off-by: George Dunlap Signed-off-by: Ronald Rojas --- tools/golang/xenlight/xenlight.go | 167 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go index 1e25413..8aaca6a 100644 --- a/tools/golang/xenlight/xenlight.go +++ b/tools/golang/xenlight/xenlight.go @@ -131,6 +131,20 @@ func hwcapCToGo(chwcap C.libxl_hwcap) (ghwcap Hwcap) { return } +// typedef struct { +// uint32_t size; /* number of bytes in map */ +// uint8_t *map; +// } libxl_bitmap; + +// Implement the Go bitmap type such that the underlying data can +// easily be copied in and out. NB that we still have to do copies +// both directions, because cgo runtime restrictions forbid passing to +// a C function a pointer to a Go-allocated structure which contains a +// pointer. +type Bitmap struct { + bitmap []C.uint8_t +} + /* * Types: IDL * @@ -198,6 +212,159 @@ type Dominfo struct { } /* + * Bitmap operations + */ + +// Return a Go bitmap which is a copy of the referred C bitmap. +func bitmapCToGo(cbm C.libxl_bitmap) (gbm Bitmap) { + // Alloc a Go slice for the bytes + size := int(cbm.size) + gbm.bitmap = make([]C.uint8_t, size) + + // Make a slice pointing to the C array + mapslice := (*[1 << 30]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size] + + // And copy the C array into the Go array + copy(gbm.bitmap, mapslice) + + return +} + +// Must be C.libxl_bitmap_dispose'd of afterwards +func bitmapGotoC(gbm Bitmap) (cbm C.libxl_bitmap) { + C.libxl_bitmap_init(&cbm) + + size := len(gbm.bitmap) + cbm._map = (*C.uint8_t)(C.malloc(C.size_t(size))) + cbm.size = C.uint32_t(size) + if cbm._map == nil { + panic("C.calloc failed!") + } + + // Make a slice pointing to the C array + mapslice := (*[1 << 30]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size] + + // And copy the Go array into the C array + copy(mapslice, gbm.bitmap) + + return +} + +func (bm *Bitmap) Test(bit int) bool { + ubit := uint(bit) + if bit > bm.Max() || bm.bitmap == nil { + return false + } + + return (bm.bitmap[bit/8] & (1 << (ubit & 7))) != 0 +} + +func (bm *Bitmap) Set(bit int) { + ibit := bit / 8 + if ibit+1 > len(bm.bitmap) { + bm.bitmap = append(bm.bitmap, make([]C.uint8_t, ibit+1-len(bm.bitmap))...) + } + + bm.bitmap[ibit] |= 1 << (uint(bit) & 7) +} + +func (bm *Bitmap) SetRange(start int, end int) { + for i := start; i <= end; i++ { + bm.Set(i) + } +} + +func (bm *Bitmap) Clear(bit int) { + ubit := uint(bit) + if bit > bm.Max() || bm.bitmap == nil { + return + } + + bm.bitmap[bit/8] &= ^(1 << (ubit & 7)) +} + +func (bm *Bitmap) ClearRange(start int, end int) { + for i := start; i <= end; i++ { + bm.Clear(i) + } +} + +func (bm *Bitmap) Max() int { + return len(bm.bitmap)*8 - 1 +} + +func (bm *Bitmap) IsEmpty() bool { + for i := 0; i < len(bm.bitmap); i++ { + if bm.bitmap[i] != 0 { + return false + } + } + return true +} + +func (a Bitmap) And(b Bitmap) (c Bitmap) { + var max, min int + if len(a.bitmap) > len(b.bitmap) { + max = len(a.bitmap) + min = len(b.bitmap) + } else { + max = len(b.bitmap) + min = len(a.bitmap) + } + c.bitmap = make([]C.uint8_t, max) + + for i := 0; i < min; i++ { + c.bitmap[i] = a.bitmap[i] & b.bitmap[i] + } + return +} + +func (bm Bitmap) String() (s string) { + lastOnline := false + crange := false + printed := false + var i int + /// --x-xxxxx-x -> 2,4-8,10 + /// --x-xxxxxxx -> 2,4-10 + for i = 0; i <= bm.Max(); i++ { + if bm.Test(i) { + if !lastOnline { + // Switching offline -> online, print this cpu + if printed { + s += "," + } + s += fmt.Sprintf("%d", i) + printed = true + } else if !crange { + // last was online, but we're not in a range; print - + crange = true + s += "-" + } else { + // last was online, we're in a range, nothing else to do + } + lastOnline = true + } else { + if lastOnline { + // Switching online->offline; do we need to end a range? + if crange { + s += fmt.Sprintf("%d", i-1) + } + } + lastOnline = false + crange = false + } + } + if lastOnline { + // Switching online->offline; do we need to end a range? + if crange { + s += fmt.Sprintf("%d", i-1) + } + } + + return +} + +/* * Context */ var Ctx Context