Full restructure in accordance with

- pkg/{subsystem}/{package} style
  - modify Makefile to reflect the new style,
    consolidate various entries
  - add a dummy ``main.go`` at top level
This commit is contained in:
Harshavardhana
2015-01-14 11:29:04 -08:00
parent 033ad56a61
commit 432275e966
178 changed files with 28 additions and 9445 deletions

36
pkg/utils/cpu/cpu.go Normal file
View File

@@ -0,0 +1,36 @@
/*
* Mini Object Storage, (C) 2014 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// +build amd64
package cpu
// int has_sse41 (void);
// int has_avx (void);
// int has_avx2 (void);
import "C"
func HasSSE41() bool {
return int(C.has_sse41()) == 1
}
func HasAVX() bool {
return int(C.has_avx()) == 1
}
func HasAVX2() bool {
return int(C.has_avx2()) == 1
}

138
pkg/utils/cpu/cpu_amd64.S Normal file
View File

@@ -0,0 +1,138 @@
/*
* Mini Object Storage, (C) 2014 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License") ;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef __APPLE__
#define HAS_SSE _has_sse41
#define HAS_AVX _has_avx
#define HAS_AVX2 _has_avx2
#else
#define HAS_SSE has_sse41
#define HAS_AVX has_avx
#define HAS_AVX2 has_avx2
#endif
.file "cpufeatures.c"
.text
cpuid:
.LFB2:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
pushq %rbx
.cfi_offset 3, -24
movq %rdi, -16(%rbp)
movl %esi, -20(%rbp)
movq -16(%rbp), %rax
leaq 4(%rax), %r10
movq -16(%rbp), %rax
leaq 8(%rax), %r9
movq -16(%rbp), %rax
leaq 12(%rax), %r8
movl -20(%rbp), %eax
movl $0, %edx
movl %edx, %ecx
#APP
# 21 "cpufeatures.c" 1
cpuid
# 0 "" 2
#NO_APP
movl %ebx, %esi
movl %eax, %edi
movq -16(%rbp), %rax
movl %edi, (%rax)
movl %esi, (%r10)
movl %ecx, (%r9)
movl %edx, (%r8)
popq %rbx
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE2:
.globl HAS_SSE
HAS_SSE:
.LFB3:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
leaq -16(%rbp), %rax
movl $1, %esi
movq %rax, %rdi
call cpuid
movl -8(%rbp), %eax
andl $524288, %eax
testl %eax, %eax
setne %al
movzbl %al, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE3:
.globl HAS_AVX
HAS_AVX:
.LFB4:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
leaq -16(%rbp), %rax
movl $1, %esi
movq %rax, %rdi
call cpuid
movl -8(%rbp), %eax
andl $268435456, %eax
testl %eax, %eax
setne %al
movzbl %al, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE4:
.globl HAS_AVX2
HAS_AVX2:
.LFB5:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
leaq -16(%rbp), %rax
movl $7, %esi
movq %rax, %rdi
call cpuid
movl -12(%rbp), %eax
andl $32, %eax
testl %eax, %eax
setne %al
movzbl %al, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc

80
pkg/utils/cpu/cpu_test.go Normal file
View File

@@ -0,0 +1,80 @@
/*
* Mini Object Storage, (C) 2014 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// +build amd64
package cpu
import (
"errors"
"os/exec"
"runtime"
"strings"
"testing"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func hasCpuFeatureFromOS(feature string) (bool, error) {
if runtime.GOOS == "linux" {
command := exec.Command("/bin/cat", "/proc/cpuinfo")
output, err := command.Output()
if err != nil {
return false, err
}
if strings.Contains(string(output), feature) {
return true, nil
} else {
return false, nil
}
} else {
// TODO find new way to test cpu flags on windows
return false, errors.New("Not Implemented on this platform")
}
}
func (s *MySuite) TestHasSSE41(c *C) {
if runtime.GOOS == "linux" {
var flag = HasSSE41()
osCheck, err := hasCpuFeatureFromOS("sse4_1")
c.Assert(err, IsNil)
c.Check(flag, Equals, osCheck)
}
}
func (s *MySuite) TestHasAVX(c *C) {
if runtime.GOOS == "linux" {
var flag = HasAVX()
osFlag, err := hasCpuFeatureFromOS("avx")
c.Assert(err, IsNil)
c.Check(osFlag, Equals, flag)
}
}
func (s *MySuite) TestHasAVX2(c *C) {
if runtime.GOOS == "linux" {
var flag = HasAVX2()
osFlag, err := hasCpuFeatureFromOS("avx2")
c.Assert(err, IsNil)
c.Check(osFlag, Equals, flag)
}
}

11
pkg/utils/cpu/doc.go Normal file
View File

@@ -0,0 +1,11 @@
// Package cpu provides wrapper around assembly functions for checking processor
// instruction capabilities for SSE4.1, AVX, AVX2 support
//
// Example
//
// ``cpu.HasSSE41()`` returns true for SSE4.1 instruction support, false otherwise
//
// ``cpu.HasAVX()`` returns true for AVX instruction support, false otherwise
//
// ``cpu.HasAVX2()`` returns true for AVX2 instruction support, false otherwise
package cpu

View File

@@ -0,0 +1,102 @@
/*
* Mini Object Storage, (C) 2014 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package unitconv
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
)
const (
UNIT_BYTE = 1 << (10 * iota)
UNIT_KILOBYTE
UNIT_MEGABYTE
UNIT_GIGABYTE
UNIT_TERABYTE
UNIT_PETABYTE
)
func BytesToString(bytes uint64) string {
var unit string = "B"
var value uint64 = 0
switch {
case bytes >= UNIT_TERABYTE:
unit = "TB"
value = uint64(bytes / UNIT_TERABYTE)
case bytes >= UNIT_GIGABYTE:
unit = "GB"
value = uint64(bytes / UNIT_GIGABYTE)
case bytes >= UNIT_MEGABYTE:
unit = "MB"
value = uint64(bytes / UNIT_MEGABYTE)
case bytes >= UNIT_KILOBYTE:
unit = "KB"
value = uint64(bytes / UNIT_KILOBYTE)
case bytes < UNIT_KILOBYTE && bytes >= UNIT_BYTE:
unit = "B"
value = uint64(bytes / UNIT_BYTE)
}
return fmt.Sprintf("%d%s", value, unit)
}
func StringToBytes(s string) (uint64, error) {
var bytes uint64
var err error
bytes, err = strconv.ParseUint(s, 10, 64)
if err == nil {
return bytes, nil
}
stringPattern, err := regexp.Compile(`(?i)^(-?\d+)([BKMGT])B?$`)
if err != nil {
return 0, err
}
parts := stringPattern.FindStringSubmatch(strings.TrimSpace(s))
if len(parts) < 2 {
return 0, errors.New("Incorrect string format must be K,KB,M,MB,G,GB")
}
value, err := strconv.ParseUint(parts[1], 10, 0)
if err != nil || value < 1 {
return 0, err
}
unit := strings.ToUpper(parts[2])
switch unit {
case "T":
bytes = value * UNIT_TERABYTE
case "G":
bytes = value * UNIT_GIGABYTE
case "M":
bytes = value * UNIT_MEGABYTE
case "K":
bytes = value * UNIT_KILOBYTE
case "B":
bytes = value * UNIT_BYTE
default:
return 0, errors.New("Incorrect string format must be K,KB,M,MB,G,GB")
}
return bytes, nil
}

View File

@@ -0,0 +1,112 @@
/*
* Mini Object Storage, (C) 2014 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package unitconv
import (
"testing"
. "gopkg.in/check.v1"
)
type MySuite struct{}
var _ = Suite(&MySuite{})
func Test(t *testing.T) { TestingT(t) }
func (s *MySuite) Test(c *C) {
value := BytesToString(100 * UNIT_BYTE)
c.Assert(value, Equals, "100B")
value = BytesToString(100 * UNIT_KILOBYTE)
c.Assert(value, Equals, "100KB")
value = BytesToString(100 * UNIT_MEGABYTE)
c.Assert(value, Equals, "100MB")
value = BytesToString(100 * UNIT_GIGABYTE)
c.Assert(value, Equals, "100GB")
value = BytesToString(100 * UNIT_TERABYTE)
c.Assert(value, Equals, "100TB")
bytes, err := StringToBytes("100B")
c.Assert(err, IsNil)
c.Assert(bytes, Equals, uint64(100))
bytes, err = StringToBytes("100")
c.Assert(err, IsNil)
c.Assert(bytes, Equals, uint64(100))
bytes, err = StringToBytes("100KB")
c.Assert(err, IsNil)
c.Assert(bytes, Equals, uint64(100*UNIT_KILOBYTE))
bytes, err = StringToBytes("100K")
c.Assert(err, IsNil)
c.Assert(bytes, Equals, uint64(100*UNIT_KILOBYTE))
bytes, err = StringToBytes("100MB")
c.Assert(err, IsNil)
c.Assert(bytes, Equals, uint64(100*UNIT_MEGABYTE))
bytes, err = StringToBytes("100M")
c.Assert(err, IsNil)
c.Assert(bytes, Equals, uint64(100*UNIT_MEGABYTE))
bytes, err = StringToBytes("100GB")
c.Assert(err, IsNil)
c.Assert(bytes, Equals, uint64(100*UNIT_GIGABYTE))
bytes, err = StringToBytes("100G")
c.Assert(err, IsNil)
c.Assert(bytes, Equals, uint64(100*UNIT_GIGABYTE))
bytes, err = StringToBytes("100TB")
c.Assert(err, IsNil)
c.Assert(bytes, Equals, uint64(100*UNIT_TERABYTE))
bytes, err = StringToBytes("100T")
c.Assert(err, IsNil)
c.Assert(bytes, Equals, uint64(100*UNIT_TERABYTE))
bytes, err = StringToBytes("0")
c.Assert(err, IsNil)
c.Assert(bytes, Equals, uint64(0))
bytes, err = StringToBytes("23")
c.Assert(err, IsNil)
c.Assert(bytes, Equals, uint64(23))
bytes, err = StringToBytes("0TB")
c.Assert(err, IsNil)
c.Assert(bytes, Equals, uint64(0))
}
func (s *MySuite) TestBadInput(c *C) {
_, err := StringToBytes("")
c.Assert(err, Not(IsNil))
_, err = StringToBytes("HELLO")
c.Assert(err, Not(IsNil))
_, err = StringToBytes("-20B")
c.Assert(err, Not(IsNil))
_, err = StringToBytes("-20MB")
c.Assert(err, Not(IsNil))
}