Documentation
¶
Overview ¶
Package qatzip implements Go bindings for the Intel® Quick Assist Technology compression library
Example (WriterReader) ¶
package main import ( "bytes" "io" "log" "os" "github.com/intel/qatgo/qatzip" ) func main() { // This is an example of compressing and decompressing using QATgo // NewWriter/NewReader provides io.Reader and io.Writer implementations compatible with Go standard library buf := new(bytes.Buffer) // contains compressed data var str = "this string will be compressed and then decompressed" zw := qatzip.NewWriter(buf) // start a compression session with buf as output err := zw.Apply( qatzip.AlgorithmOption(qatzip.DEFLATE), // set DEFLATE algorithm (lz4 and zstd are supported as well) qatzip.DeflateFmtOption(qatzip.DeflateGzipExt), // set format to gzip extended header (for DEFLATE algorithm only) qatzip.CompressionLevelOption(6), // set compression level 6 ) if err != nil { log.Fatal("error: could not apply compression options:", err) } // compress input string and output to buf if _, err = zw.Write([]byte(str)); err != nil { log.Fatal("error: could not compress input:", err) } // end compression session: flush remaining input buffer and release resources if err = zw.Close(); err != nil { log.Fatal("error: could not end compression session:", err) } zr, err := qatzip.NewReader(buf) // start a decompression session with buf as input if err != nil { log.Fatal("error: could not start decompression session:", err) } err = zr.Apply( qatzip.AlgorithmOption(qatzip.DEFLATE), // enable DEFLATE qatzip.DeflateFmtOption(qatzip.DeflateGzipExt), // enable gzip extended header ) if err != nil { log.Fatal("error: could not apply decompression options:", err) } // decompress input stream and write to stdout if _, err = io.Copy(os.Stdout, zr); err != nil { log.Fatal("error: could not decompress input:", err) } // end decompression session: release accelerator resources if err = zr.Close(); err != nil { log.Fatal("error: could not end decompression session:", err) } }
Output: this string will be compressed and then decompressed
Example (WriterReaderDirect) ¶
package main import ( "bytes" "fmt" "log" "strings" "github.com/intel/qatgo/qatzip" ) func main() { // This is an example of compressing and decompressing using QATgo's direct methods // Direct methods provide direct access to low-level QAT APIs without any high-level QATgo buffer management // The methods only supports byte slices for I/O // The decompressed data buffer in this example is too small requiring the buffer to be resized var str = strings.Repeat("string repeats..", 64) cbuf := make([]byte, len(str)+512) // contains compressed data dbuf := make([]byte, 512) // contains decompressed data (will need resizing) q, err := qatzip.NewQzBinding() // instantiate direct session object if err != nil { log.Fatal("error: could not instantiate direct compression session:", err) } err = q.Apply( qatzip.AlgorithmOption(qatzip.DEFLATE), // enable DEFLATE (lz4 and zstd are supported as well) qatzip.DeflateFmtOption(qatzip.DeflateGzipExt), // enable gzip extended header (for DEFLATE algorithm only) qatzip.DirOption(qatzip.Compress), // set direction to compress qatzip.CompressionLevelOption(6), // set compression level 6 ) if err != nil { log.Fatal("error: could not apply compression options:", err) } // start new compression session if err = q.StartSession(); err != nil { log.Fatal("error: could not start compression session:", err) } // Only one buffer is being compressed, so mark this as the last buffer q.SetLast(true) // compress input string and output to cbuf, nc is bytes output during compression _, nc, err := q.Compress([]byte(str), cbuf) if err != nil { log.Fatal("error: could not compress input data:", err) } // end compression session and release resources if err = q.Close(); err != nil { log.Fatal("error: could not end input session:", err) } q, err = qatzip.NewQzBinding() // instantiate a new direct session object if err != nil { log.Fatal("error: could not instatiate direct decompression session:", err) } err = q.Apply( qatzip.AlgorithmOption(qatzip.DEFLATE), // enable DEFLATE (must match compress) qatzip.DeflateFmtOption(qatzip.DeflateGzipExt), // enable gzip extended header qatzip.DirOption(qatzip.Decompress), // set direction to decompress ) if err != nil { log.Fatal("error: could not apply compression options:", err) } // start new compression session if err = q.StartSession(); err != nil { log.Fatal("error: could not start direct decompression session:", err) } // decompress input stream and write to stdout, nd = bytes output during decompress // in this example the size of the output buffer is too small and will need to be grown var nd int for { if _, nd, err = q.Decompress(cbuf[:nc], dbuf); err != nil { // if output buffer is not long enough grow output buffer // ErrBuffer means there is insufficient buffer space for output if err == qatzip.ErrBuffer { dbuf = make([]byte, len(dbuf)*2) // double buffer size and try again continue } log.Fatal("error: could not decompress input data:", err) } // decompression was successful break } // end decompression session and release accelerator resources if err = q.Close(); err != nil { log.Fatal("error: could not end decompression session:", err) } // verify that the decompressed output matches the original input if bytes.Equal(dbuf[:nd], []byte(str)) { fmt.Println("strings match") } else { fmt.Println("strings do not match") } }
Output: strings match
Index ¶
- Constants
- Variables
- func Error(errorCode int) (err error)
- type Algorithm
- type DebugLevel
- type DeflateFmt
- type Direction
- type HuffmanHdr
- type InputBufferMode
- type Option
- func AlgorithmOption(alg Algorithm) Option
- func BounceBufferLengthOption(len int) Option
- func BufferGrowthOption(len int) Option
- func CompressionLevelOption(level int) Option
- func DebugLevelOption(level DebugLevel) Option
- func DeflateFmtOption(fmt DeflateFmt) Option
- func DirOption(dir Direction) Option
- func HuffmanHdrOption(hdrType HuffmanHdr) Option
- func HwBufSizeOption(size int) Option
- func InputBufLengthOption(len int) Option
- func InputBufferModeOption(mode InputBufferMode) Option
- func MaxBufferLengthOption(n int) Option
- func MaxForksOption(max int) Option
- func OutputBufLengthOption(len int) Option
- func PollingModeOption(mode PollingMode) Option
- func ReqCountThresholdOption(n int) Option
- func SensitiveOption(enable bool) Option
- func StreamBufSizeOption(size int) Option
- func SwBackupOption(enable bool) Option
- func SwSwitchThresholdOption(size int) Option
- func WaitCountThresholdOption(n int) Option
- type Perf
- type PollingMode
- type QzBinding
- func (q *QzBinding) Apply(options ...Option) (err error)
- func (q *QzBinding) Close() (err error)
- func (q *QzBinding) Compress(in []byte, out []byte) (c int, p int, err error)
- func (q *QzBinding) CompressCRC(in []byte, out []byte, crc *uint64) (c int, p int, err error)
- func (q *QzBinding) Decompress(in []byte, out []byte) (c int, p int, err error)
- func (q *QzBinding) SetLast(enable bool)
- func (q *QzBinding) StartSession() (err error)
- type Reader
- type Writer
Examples ¶
Constants ¶
const ( DEFLATE_ID uint8 = C.QZ_DEFLATE LZ4_ID uint8 = C.QZ_LZ4 )
const ( DefaultCompression = 1 MinBufferLength = 128 * 1024 DefaultMaxBufferLength = 2 * 1024 * 1024 * 1024 DefaultBufferLength = 128 * 1024 * 1024 DefaultBufferGrowth = 128 * 1024 * 1024 DefaultBounceBufferLength = 512 MinBounceBufferLength = 512 )
Variables ¶
var ( QatErrHdr = string("QATzip error: ") ErrParams = errors.New(QatErrHdr + "invalid parameter in function call") ErrFail = errors.New(QatErrHdr + "unspecified error") ErrBuffer = errors.New(QatErrHdr + "insufficient buffer error") ErrData = errors.New(QatErrHdr + "input data was corrupted") ErrTimeout = errors.New(QatErrHdr + "operation timed out") ErrIntegrity = errors.New(QatErrHdr + "integrity checked failed") ErrNoHw = errors.New(QatErrHdr + "using SW: No QAT HW detected") ErrNoMDrv = errors.New(QatErrHdr + "using SW: No memory driver detected") ErrNoInstAttached = errors.New(QatErrHdr + "using SW: Could not attach to an instance") ErrLowMem = errors.New(QatErrHdr + "using SW: Not enough pinned memory") ErrLowDestMem = errors.New(QatErrHdr + "using SW: Not enough pinned memory for dest buffer") ErrUnsupportedFmt = errors.New(QatErrHdr + "using SW: QAT device does not support data format") ErrNone = errors.New(QatErrHdr + "device uninitialized") ErrNoSwHw = errors.New(QatErrHdr + "not using SW: No QAT HW detected") ErrNoSwMDrv = errors.New(QatErrHdr + "not using SW: No memory driver detected") ErrNoSwNoInst = errors.New(QatErrHdr + "not using SW: Could not attach to instance") ErrNoSwLowMem = errors.New(QatErrHdr + "not using SW: not enough pinned memory") ErrNoSwAvail = errors.New(QatErrHdr + "session may require software, but no software is available") ErrNoSwUnsupportedFmt = errors.New(QatErrHdr + "not using SW: QAT device does not support data format") ErrPostProcess = errors.New(QatErrHdr + "using post process: post process callback encountered an error") ErrMetaDataOverflow = errors.New(QatErrHdr + "insufficent memory allocated for metadata") ErrOutOfRange = errors.New(QatErrHdr + "metadata block_num specified is out of range") ErrNotSupported = errors.New(QatErrHdr + "request not supported") ErrParamOutputBufLength = errors.New(QatErrHdr + "invalid size for output buffer length") ErrParamInputBufLength = errors.New(QatErrHdr + "invalid size for input buffer length") ErrParamBufferGrowth = errors.New(QatErrHdr + "invalid size for buffer Growth") ErrParamBounceBufferLength = errors.New(QatErrHdr + "invalid size for bounce buffer length") ErrParamAlgorithm = errors.New(QatErrHdr + "invalid algorithm type") ErrParamDirection = errors.New(QatErrHdr + "invalid direction") ErrParamDataFmtDeflate = errors.New(QatErrHdr + "invalid deflate format type") ErrParamHuffmanHdr = errors.New(QatErrHdr + "invalid huffman header type") ErrParamPollingMode = errors.New(QatErrHdr + "invalid polling mode") ErrParamCompressionLevel = errors.New(QatErrHdr + "invalid compression level") ErrWriterClosed = errors.New(QatErrHdr + "cannot write to closed writer") ErrEmptyBuffer = errors.New(QatErrHdr + "empty buffer") ErrNoMem = errors.New(QatErrHdr + "out of memory") ErrInputBufferMode = errors.New(QatErrHdr + "invalid input buffer mode") ErrApplyPostInit = errors.New(QatErrHdr + "cannot apply options after Reset() or I/O") ErrApplyInvalidType = errors.New(QatErrHdr + "option appied to incorrect type") ErrMaxBufferGrowth = errors.New(QatErrHdr + "cannot grow buffer large enough") ErrParamMaxBufferLength = errors.New(QatErrHdr + "invalid size for max buffer length") )
Functions ¶
Types ¶
type DeflateFmt ¶
type DeflateFmt int
const ( /* QzDataFormat_T */ Deflate48 DeflateFmt = iota DeflateGzip DeflateGzipExt DeflateRaw )
The following enums must exactly match the equivalent Enum type in QATzip.h
type HuffmanHdr ¶
type HuffmanHdr int
const ( /* QzHuffmanHdr_T */ Dynamic HuffmanHdr = iota Static )
type InputBufferMode ¶
type InputBufferMode int
const ( Reserve InputBufferMode = iota // Reserve a portion of the input buffer for last Bounce // Bounce all buffers Last // Force last=true for all buffers NoLast // Force last=false for all buffers )
type Option ¶
type Option func(a applier) error
func AlgorithmOption ¶
Algorithm setting [DEFLATE, lz4, zstd]
func BounceBufferLengthOption ¶
Length of the bounce buffer (Writer)
func BufferGrowthOption ¶
If output buffer is too small (see QZ_BUF_ERROR) increase size of output buffer by len and retry (Reader/Writer)
func CompressionLevelOption ¶
Compression level setting (Writer)
func DebugLevelOption ¶
func DebugLevelOption(level DebugLevel) Option
Debug level option [None, Low, Med, High, Debug]
func DeflateFmtOption ¶
func DeflateFmtOption(fmt DeflateFmt) Option
DEFLATE format setting [raw, gzip, gzip extended]
func HuffmanHdrOption ¶
func HuffmanHdrOption(hdrType HuffmanHdr) Option
Dynamic or static Huffman headers setting
func HwBufSizeOption ¶
Hardware buffer size [must be a power of 2KB]
func InputBufferModeOption ¶
func InputBufferModeOption(mode InputBufferMode) Option
Input buffer mode setting (Writer) [Reserve, Bounce, Last, NoLast]
func MaxBufferLengthOption ¶ added in v1.1.0
Max size an output buffer can grow to in bytes
func MaxForksOption ¶
Maximum forks permitted in the current thread
func OutputBufLengthOption ¶
Output buffer size (Reader, Writer)
func PollingModeOption ¶
func PollingModeOption(mode PollingMode) Option
Polling mode setting [periodical, busy]
func ReqCountThresholdOption ¶
Threshold for how many buffer requests QATzip can make on a single thread
func StreamBufSizeOption ¶
Stream buffer size between [1KB .. 2MB - 5KB]
func SwSwitchThresholdOption ¶
If input size is less than threshold size switch to SW
func WaitCountThresholdOption ¶
Retry count for QATzip initialization if failure occurs
type Perf ¶
type Perf struct { ReadTimeNS uint64 // time (ns) spent reading from r WriteTimeNS uint64 // time (ns) spent writing to w BytesIn uint64 // bytes sent to QATzip BytesOut uint64 // bytes received from QATzip EngineTimeNS uint64 // time (ns) spent in QATzip CopyTimeNS uint64 // time (ns) spent copying buffers + reallocation }
Performance counters
type PollingMode ¶
type PollingMode int
const ( /* QzPollingMode_T */ Periodical PollingMode = iota Busy )
type QzBinding ¶
type QzBinding struct {
// contains filtered or unexported fields
}
Manages QATzip session state
func (*QzBinding) Compress ¶
QATzip compress (in = input buffer, out = output buffer, c = consumed, p = produced)
func (*QzBinding) CompressCRC ¶
QATzip compress with CRC (in = input buffer, out = output buffer, crc = crc value, c = consumed, p = produced)
func (*QzBinding) Decompress ¶
QATzip decompress (in = input buffer, out = output buffer, c = consumed, p = produced)
func (*QzBinding) StartSession ¶
Start QATzip session
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader implements an io.Reader. When read from, it decompresses content from r.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer implements an io.Writer. When written to, it sends compressed content to w.
func NewWriterLevel ¶
NewWriterLevel creates a new Writer with an additional compression level setting