Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / erasure-code / shec / ErasureCodeShecTableCache.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2014 FUJITSU LIMITED
7  * Copyright (C) 2014 CERN (Switzerland)
8  *
9  * Author: Takanori Nakao <nakao.takanori@jp.fujitsu.com>
10  * Author: Takeshi Miyamae <miyamae.takeshi@jp.fujitsu.com>
11  * Author: Andreas-Joachim Peters <Andreas.Joachim.Peters@cern.ch>
12  *
13  *  This library is free software; you can redistribute it and/or
14  *  modify it under the terms of the GNU Lesser General Public
15  *  License as published by the Free Software Foundation; either
16  *  version 2.1 of the License, or (at your option) any later version.
17  *
18  */
19
20 #ifndef CEPH_ERASURE_CODE_SHEC_TABLE_CACHE_H
21 #define CEPH_ERASURE_CODE_SHEC_TABLE_CACHE_H
22
23 // -----------------------------------------------------------------------------
24 #include "common/Mutex.h"
25 #include "erasure-code/ErasureCodeInterface.h"
26 // -----------------------------------------------------------------------------
27 #include <list>
28 // -----------------------------------------------------------------------------
29
30 class ErasureCodeShecTableCache {
31   // ---------------------------------------------------------------------------
32   // This class implements a table cache for encoding and decoding matrices.
33   // Encoding matrices are shared for the same (k,m,c,w) combination.
34   // It supplies a decoding matrix lru cache which is shared for identical
35   // matrix types e.g. there is one cache (lru-list + lru-map)
36   // ---------------------------------------------------------------------------
37
38   class DecodingCacheParameter {
39    public:
40     int* decoding_matrix;  // size: k*k
41     int* dm_row;  // size: k
42     int* dm_column;  // size: k
43     int* minimum;  // size: k+m
44     DecodingCacheParameter() {
45       decoding_matrix = 0;
46       dm_row = 0;
47       dm_column = 0;
48       minimum = 0;
49     }
50     ~DecodingCacheParameter() {
51       if (decoding_matrix) {
52         delete[] decoding_matrix;
53       }
54       if (dm_row) {
55         delete[] dm_row;
56       }
57       if (dm_column) {
58         delete[] dm_column;
59       }
60       if (minimum) {
61         delete[] minimum;
62       }
63     }
64   };
65
66  public:
67
68   static const int decoding_tables_lru_length = 10000;
69   typedef std::pair<std::list<uint64_t>::iterator,
70                     DecodingCacheParameter> lru_entry_t;
71   typedef std::map< int, int** > codec_table_t;
72   typedef std::map< int, codec_table_t > codec_tables_t__;
73   typedef std::map< int, codec_tables_t__ > codec_tables_t_;
74   typedef std::map< int, codec_tables_t_ > codec_tables_t;
75   typedef std::map< int, codec_tables_t > codec_technique_tables_t;
76   // int** matrix = codec_technique_tables_t[technique][k][m][c][w]
77   
78   typedef std::map< uint64_t, lru_entry_t > lru_map_t;
79   typedef std::list< uint64_t > lru_list_t;
80
81  ErasureCodeShecTableCache() :
82   codec_tables_guard("shec-lru-cache")
83     {
84     }
85   
86   virtual ~ErasureCodeShecTableCache();
87   
88   Mutex codec_tables_guard; // mutex used to protect modifications in encoding/decoding table maps
89   
90   bool getDecodingTableFromCache(int* matrix,
91                                  int* dm_row, int* dm_column,
92                                  int* minimum,
93                                  int technique,
94                                  int k, int m, int c, int w,
95                                  int* want, int* avails);
96
97   void putDecodingTableToCache(int* matrix,
98                                int* dm_row, int* dm_column,
99                                int* minimum,
100                                int technique,
101                                int k, int m, int c, int w,
102                                int* want, int* avails);
103
104   int** getEncodingTable(int technique, int k, int m, int c, int w);
105   int** getEncodingTableNoLock(int technique, int k, int m, int c, int w);
106   int* setEncodingTable(int technique, int k, int m, int c, int w, int*);
107   
108  private:
109   // encoding table accessed via table[matrix][k][m][c][w]
110   // decoding table cache accessed via map[matrixtype]
111   // decoding table lru list accessed via list[matrixtype]
112   codec_technique_tables_t encoding_table;
113   std::map<int, lru_map_t*> decoding_tables;
114   std::map<int, lru_list_t*> decoding_tables_lru;
115
116   lru_map_t* getDecodingTables(int technique);
117   lru_list_t* getDecodingTablesLru(int technique);
118   uint64_t getDecodingCacheSignature(int k, int m, int c, int w,
119                                      int *want, int *avails);
120
121   Mutex* getLock();
122 };
123
124 #endif