Skip to content

Commit 1016989

Browse files
committed
Add block queue rotational parsing
1 parent 0e9ab6e commit 1016989

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed

include/pfs/block.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define PFS_BLOCK_HPP
1919

2020
#include "types.hpp"
21+
#include "block_queue.hpp"
2122

2223
namespace pfs {
2324

@@ -40,6 +41,7 @@ class block final
4041
dev_t get_dev() const;
4142

4243
block_stat get_stat() const;
44+
block_queue get_queue() const;
4345

4446
public: // Getters
4547

include/pfs/block_queue.hpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2020-present Daniel Trugman
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef PFS_BLOCK_QUEUE_HPP
18+
#define PFS_BLOCK_QUEUE_HPP
19+
20+
#include "types.hpp"
21+
22+
namespace pfs {
23+
24+
// Hint: See 'https://www.kernel.org/doc/Documentation/block/queue-sysfs.txt'
25+
class block_queue final
26+
{
27+
public:
28+
block_queue(const block_queue&) = default;
29+
block_queue(block_queue&&) = default;
30+
31+
block_queue& operator=(const block_queue&) = delete;
32+
block_queue& operator=(block_queue&&) = delete;
33+
34+
public: // Properties
35+
const std::string& dir() const;
36+
37+
public: // Getters
38+
bool get_rotational() const;
39+
40+
private:
41+
friend class block;
42+
block_queue(const std::string& block_root);
43+
44+
private:
45+
static std::string build_block_queue_root(const std::string& block_root);
46+
47+
private:
48+
static const std::string QUEUE_DIR;
49+
50+
private:
51+
const std::string _block_queue_root;
52+
};
53+
54+
} // namespace pfs
55+
56+
#endif // PFS_BLOCK_HPP

sample/enum_block.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ static void enum_block(const pfs::block& block)
3636

3737
auto stat = block.get_stat();
3838
print(stat);
39+
40+
auto queue = block.get_queue();
41+
print(queue);
3942
}
4043
catch (const std::runtime_error& ex)
4144
{

sample/format.hpp

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <sstream>
2020

2121
#include "pfs/procfs.hpp"
22+
#include "pfs/sysfs.hpp"
2223

2324
template <typename T>
2425
inline std::string join(const T& container)
@@ -748,3 +749,12 @@ inline std::ostream& operator<<(std::ostream& out,
748749

749750
return out;
750751
}
752+
753+
inline std::ostream& operator<<(std::ostream& out,
754+
const pfs::block_queue& queue)
755+
{
756+
out << std::boolalpha;
757+
out << "rotational[" << queue.get_rotational() << "] ";
758+
759+
return out;
760+
}

src/block.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,9 @@ block_stat block::get_stat() const
8383
return parsers::parse_block_stat_line(line);
8484
}
8585

86+
block_queue block::get_queue() const
87+
{
88+
return block_queue(_block_root);
89+
}
90+
8691
} // namespace pfs

src/block_queue.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2020-present Daniel Trugman
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <string>
18+
19+
#include "pfs/defer.hpp"
20+
#include "pfs/parsers/common.hpp"
21+
#include "pfs/parsers/generic.hpp"
22+
#include "pfs/block_queue.hpp"
23+
24+
namespace pfs {
25+
26+
using namespace impl;
27+
28+
const std::string block_queue::QUEUE_DIR("queue/");
29+
30+
block_queue::block_queue(const std::string& block_root)
31+
: _block_queue_root(build_block_queue_root(block_root))
32+
{}
33+
34+
std::string block_queue::build_block_queue_root(const std::string& block_root)
35+
{
36+
return block_root + QUEUE_DIR;
37+
}
38+
39+
const std::string& block_queue::dir() const
40+
{
41+
return _block_queue_root;
42+
}
43+
44+
bool block_queue::get_rotational() const
45+
{
46+
static const std::string SIZE_FILE("rotational");
47+
auto path = _block_queue_root + SIZE_FILE;
48+
49+
auto value = utils::readline(path);
50+
51+
int number;
52+
parsers::to_number(value, number, utils::base::decimal);
53+
return number != 0;
54+
}
55+
56+
} // namespace pfs

0 commit comments

Comments
 (0)