<commit_before>/*
 * node.cpp
 * Copyright (C) 2015 Chris Waltrip <walt2178@vandals.uidaho.edu>
 *
 * This file is part of EvoComp-SantaFe
 *
 * EvoComp-SantaFe is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * EvoComp-SantaFe is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with EvoComp-SantaFe.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <deque>
#include <iostream> /* Logging/error reporting only */
#include <string> /* for std::to_string() which may not be needed. */
#include <sstream>
#include "node.h"

void Node::Copy(Node *to_copy) {
	parent_ = to_copy->parent_;
	op_ = to_copy->op_;

	children_.clear();

	switch (op_) {
	case OpType::kProg3:
		children_.resize(3);
		for (size_t i = 0; i < 3; ++i) {
			children_[i] = new Node;
			children_[i]->Copy(to_copy->children_[i]);
		}
		break;
	case OpType::kProg2:
		children_.resize(2);
		for (size_t i = 0; i < 2; ++i) {
			children_[i] = new Node;
			children_[i]->Copy(to_copy->children_[i]);
		}
		break;
	case OpType::kIfFoodAhead:
		children_.resize(1);
		children_[0] = new Node;
		children_[0]->Copy(to_copy->children_[0]);
		break;
	}
}
void Node::Erase() {
	for (Node *child : children_) {
		if (child) {
			child->Erase();
		}
	}
	delete this;
}
std::string Node::ToString(bool latex) {
	switch (op_) {
	case OpType::kProg3:
		return("(" + children_[0]->ToString(latex) + ")\n" +
			   "(" + children_[1]->ToString(latex) + ")\n" +
			   "(" + children_[2]->ToString(latex) + ")");
	case OpType::kProg2:
		return("(" + children_[0]->ToString(latex) + ")\n" +
			   "(" + children_[1]->ToString(latex) + ")");
	case OpType::kIfFoodAhead:
		return("{" + children_[0]->ToString(latex) + "}");
	case OpType::kMoveForward:
		return "^";
	case OpType::kTurnLeft:
		return "<";
	case OpType::kTurnRight:
		return ">";
	default:
		return "!";
	}
}
void Node::GenerateTree(size_t cur_depth, size_t max_depth,
						Node *parent, bool full_tree) {
	OpType lower_bound, upper_bound;
	parent_ = parent;

	if (full_tree) {
		lower_bound = OpType::kProg3;
		upper_bound = OpType::kIfFoodAhead;
	} else {
		lower_bound = OpType::kProg3;
		upper_bound = OpType::kTurnRight;
	}
	if (cur_depth >= max_depth) {
		lower_bound = OpType::kMoveForward;
		upper_bound = OpType::kTurnRight;
	}

	std::uniform_int_distribution<int> d{ lower_bound, upper_bound };
	op_ = static_cast<OpType>(d(GetEngine()));
	size_t children_counter = 0;
	children_.clear(); /** @todo	Will this damage crossover? */
	switch (op_) {
	case OpType::kProg3:
		++children_counter;
	case OpType::kProg2:
		++children_counter;
	case OpType::kIfFoodAhead:
		++children_counter;
		children_.resize(children_counter);
		for (size_t i = 0; i < children_counter; ++i) {
			Node *child = new Node;
			children_[i] = child;
			child->GenerateTree(cur_depth + 1, max_depth, this, full_tree);
		}
		break;
	case OpType::kMoveForward:
	case OpType::kTurnLeft:
	case OpType::kTurnRight:
		break;
	default:
		/* Shouldn't ever get here */
		std::cerr << "Bad node type!" << std::endl;
		exit(EXIT_FAILURE);
		break;
	}
}
void Node::Mutate(double mutation_chance, size_t max_depth) {
	std::uniform_real_distribution<double> mut_dist{ 0,1 };
	const size_t kMinimumTreeIncrease = 3;

	if (mut_dist(GetEngine()) <= mutation_chance) {
		OpType lower_bound, upper_bound;
		if (IsTerminal()) {
			lower_bound = OpType::kMoveForward;
			upper_bound = OpType::kTurnRight;
		} else {
			lower_bound = OpType::kProg3;
			upper_bound = OpType::kIfFoodAhead;
		}
		std::uniform_int_distribution<int> d{ lower_bound, upper_bound };
		op_ = static_cast<OpType>(d(GetEngine()));
		size_t children_counter = 0;
		children_.clear();
		switch (op_) {
		case OpType::kProg3:
			++children_counter;
		case OpType::kProg2:
			++children_counter;
		case OpType::kIfFoodAhead:
			++children_counter;
			for (size_t i = 0; i < children_counter; ++i) {
				Node *child = new Node;
				children_[i] = child;
				/* Determine max tree size */
				size_t adjusted_depth = max_depth;
				if (adjusted_depth < depth_) {
					adjusted_depth = depth_ + kMinimumTreeIncrease;
				}
				child->GenerateTree(depth_ + 1, adjusted_depth, this, false);
			}
		}
	} else {
		for (Node *child : children_) {
			child->Mutate(mutation_chance, max_depth);
		}
	}
}
void Node::Evaluate(TrailMap &map) {
	switch (op_) {
	case OpType::kProg3:
		for (size_t i = 0; i < 3; ++i) {
			children_[i]->Evaluate(map);
		}
		break;
	case OpType::kProg2:
		for (size_t i = 0; i < 2; ++i) {
			children_[i]->Evaluate(map);
		}
		break;
	case OpType::kIfFoodAhead:
		if (map.IsFoodAhead()) {
			children_.front()->Evaluate(map);
		}
		break;
	case OpType::kMoveForward:
		map.MoveForward();
		break;
	case OpType::kTurnLeft:
		map.TurnLeft();
		break;
	case OpType::kTurnRight:
		map.TurnRight();
		break;
	}
}
std::pair<Node*, size_t> Node::SelectNode(size_t countdown, bool nonterminal) {
	std::deque<std::pair<Node*, size_t>> stack;
	std::pair<Node*, size_t> curr;
	bool done = false;

	stack.emplace_front(std::make_pair(this, 0));

	while (!done) {
		if (stack.empty()) {
			std::cerr << "Empty stack!" << std::endl;
			exit(EXIT_FAILURE);
		}
		if (countdown == SIZE_MAX) {
			std::cerr << "Countdown wrapped around!" << std::endl;
			exit(EXIT_FAILURE);
		}
		curr = stack.front();
		stack.pop_front();
		if (countdown == 0 && nonterminal == curr.first->IsNonterminal()) {
			/* Found the correct node */
			done = true;
			break;
		}
		if (nonterminal == curr.first->IsNonterminal()) {
			--countdown;
		}
		for (size_t i = 0; i < curr.first->children_.size(); ++i) {
			stack.emplace_front(std::make_pair(curr.first->children_[i], i));
		}
	}
	return curr;
}

void Node::CountNodes(size_t &term_count, size_t &nonterm_count) {
	switch (op_) {
	case OpType::kProg3:
	case OpType::kProg2:
	case OpType::kIfFoodAhead:
		++nonterm_count;
		for (Node* child : children_) {
			child->CountNodes(term_count, nonterm_count);
		}
		break;
	case OpType::kMoveForward:
	case OpType::kTurnLeft:
	case OpType::kTurnRight:
		++term_count;
		break;
	}
}

bool Node::IsNonterminal() {
	switch (op_) {
	case OpType::kProg3:
	case OpType::kProg2:
	case OpType::kIfFoodAhead:
		return true;
	}
	return false;
}

bool Node::IsTerminal() {
	return !IsNonterminal();
}

void Node::CorrectNodes(Node *parent, size_t depth) {
	this->parent_ = parent;
	this->depth_ = depth;
	if (IsNonterminal()) {
		for (Node *child : children_) {
			child->CorrectNodes(this, depth + 1);
		}
	}
}

Node* Node::GetParent() {
	return parent_;
}
Node* Node::GetChild(size_t child_number) {
	if (child_number < children_.size()) {
		return children_[child_number];
	}
	return nullptr;
}
void Node::SetParent(Node *parent) {
	parent_ = parent;
}
void Node::SetChild(size_t child_number, Node *child) {
	if (child_number < children_.size()) {
		children_[child_number] = child;
	}
	children_.push_back(child);
}
size_t Node::GetCurrentDepth() {
	return depth_;
}
std::mt19937 &Node::GetEngine() {
	static std::random_device rd;
	static std::mt19937 mt(rd());
	return mt;
}<commit_msg>Completely forgot that IfFoodAhead actually needs a second branch for the 'if false' part of the operator type.  Now infinite loops won't occur!<commit_after>/*
 * node.cpp
 * Copyright (C) 2015 Chris Waltrip <walt2178@vandals.uidaho.edu>
 *
 * This file is part of EvoComp-SantaFe
 *
 * EvoComp-SantaFe is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * EvoComp-SantaFe is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with EvoComp-SantaFe.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <deque>
#include <iostream> /* Logging/error reporting only */
#include <string> /* for std::to_string() which may not be needed. */
#include <sstream>
#include "node.h"

void Node::Copy(Node *to_copy) {
	parent_ = to_copy->parent_;
	op_ = to_copy->op_;

	children_.clear();

	switch (op_) {
	case OpType::kProg3:
	case OpType::kProg2:
	case OpType::kIfFoodAhead:
		children_.resize(to_copy->children_.size());
		for (size_t i = 0; i < to_copy->children_.size(); ++i) {
			children_[i] = new Node;
			children_[i]->Copy(to_copy->children_[i]);
		}
		break;
	}
}
void Node::Erase() {
	for (Node *child : children_) {
		if (child) {
			child->Erase();
		}
	}
	delete this;
}
std::string Node::ToString(bool latex) {
	switch (op_) {
	case OpType::kProg3:
		return("(" + children_[0]->ToString(latex) + ")\n" +
			   "(" + children_[1]->ToString(latex) + ")\n" +
			   "(" + children_[2]->ToString(latex) + ")");
	case OpType::kProg2:
		return("(" + children_[0]->ToString(latex) + ")\n" +
			   "(" + children_[1]->ToString(latex) + ")");
	case OpType::kIfFoodAhead:
		return("{" + children_[0]->ToString(latex) + "}");
	case OpType::kMoveForward:
		return "^";
	case OpType::kTurnLeft:
		return "<";
	case OpType::kTurnRight:
		return ">";
	default:
		return "!";
	}
}
void Node::GenerateTree(size_t cur_depth, size_t max_depth,
						Node *parent, bool full_tree) {
	OpType lower_bound, upper_bound;
	parent_ = parent;

	if (full_tree) {
		lower_bound = OpType::kProg3;
		upper_bound = OpType::kIfFoodAhead;
	} else {
		lower_bound = OpType::kProg3;
		upper_bound = OpType::kTurnRight;
	}
	if (cur_depth >= max_depth) {
		lower_bound = OpType::kMoveForward;
		upper_bound = OpType::kTurnRight;
	}

	std::uniform_int_distribution<int> d{ lower_bound, upper_bound };
	op_ = static_cast<OpType>(d(GetEngine()));
	size_t children_counter = 0;
	children_.clear(); /** @todo	Will this damage crossover? */
	switch (op_) {
	case OpType::kProg3:
		++children_counter;
	case OpType::kProg2:
	case OpType::kIfFoodAhead:
		children_counter += 2;
		children_.resize(children_counter);
		for (size_t i = 0; i < children_counter; ++i) {
			Node *child = new Node;
			children_[i] = child;
			child->GenerateTree(cur_depth + 1, max_depth, this, full_tree);
		}
		break;
	case OpType::kMoveForward:
	case OpType::kTurnLeft:
	case OpType::kTurnRight:
		break;
	default:
		/* Shouldn't ever get here */
		std::cerr << "Bad node type!" << std::endl;
		exit(EXIT_FAILURE);
		break;
	}
}
void Node::Mutate(double mutation_chance, size_t max_depth) {
	std::uniform_real_distribution<double> mut_dist{ 0,1 };
	const size_t kMinimumTreeIncrease = 3;

	if (mut_dist(GetEngine()) <= mutation_chance) {
		OpType lower_bound, upper_bound;
		if (IsTerminal()) {
			lower_bound = OpType::kMoveForward;
			upper_bound = OpType::kTurnRight;
		} else {
			lower_bound = OpType::kProg3;
			upper_bound = OpType::kIfFoodAhead;
		}
		std::uniform_int_distribution<int> d{ lower_bound, upper_bound };
		op_ = static_cast<OpType>(d(GetEngine()));
		size_t children_counter = 0;
		children_.clear();
		switch (op_) {
		case OpType::kProg3:
			++children_counter;
		case OpType::kProg2:
		case OpType::kIfFoodAhead:
			children_counter += 2;
			for (size_t i = 0; i < children_counter; ++i) {
				Node *child = new Node;
				children_[i] = child;
				/* Determine max tree size */
				size_t adjusted_depth = max_depth;
				if (adjusted_depth < depth_) {
					adjusted_depth = depth_ + kMinimumTreeIncrease;
				}
				child->GenerateTree(depth_ + 1, adjusted_depth, this, false);
			}
		}
	} else {
		for (Node *child : children_) {
			child->Mutate(mutation_chance, max_depth);
		}
	}
}
void Node::Evaluate(TrailMap &map) {
	/** @todo	Combone kProg3 and kProg2 and iterate over size of children */
	switch (op_) {
	case OpType::kProg3:
		for (size_t i = 0; i < 3; ++i) {
			children_[i]->Evaluate(map);
		}
		break;
	case OpType::kProg2:
		for (size_t i = 0; i < 2; ++i) {
			children_[i]->Evaluate(map);
		}
		break;
	case OpType::kIfFoodAhead:
		if (map.IsFoodAhead()) {
			children_[0]->Evaluate(map);
		} else {
			children_[1]->Evaluate(map);
		}
		break;
	case OpType::kMoveForward:
		map.MoveForward();
		break;
	case OpType::kTurnLeft:
		map.TurnLeft();
		break;
	case OpType::kTurnRight:
		map.TurnRight();
		break;
	}
}
std::pair<Node*, size_t> Node::SelectNode(size_t countdown, bool nonterminal) {
	std::deque<std::pair<Node*, size_t>> stack;
	std::pair<Node*, size_t> curr;
	bool done = false;

	stack.emplace_front(std::make_pair(this, 0));

	while (!done) {
		if (stack.empty()) {
			std::cerr << "Empty stack!" << std::endl;
			exit(EXIT_FAILURE);
		}
		if (countdown == SIZE_MAX) {
			std::cerr << "Countdown wrapped around!" << std::endl;
			exit(EXIT_FAILURE);
		}
		curr = stack.front();
		stack.pop_front();
		if (countdown == 0 && nonterminal == curr.first->IsNonterminal()) {
			/* Found the correct node */
			done = true;
			break;
		}
		if (nonterminal == curr.first->IsNonterminal()) {
			--countdown;
		}
		for (size_t i = 0; i < curr.first->children_.size(); ++i) {
			stack.emplace_front(std::make_pair(curr.first->children_[i], i));
		}
	}
	return curr;
}

void Node::CountNodes(size_t &term_count, size_t &nonterm_count) {
	switch (op_) {
	case OpType::kProg3:
	case OpType::kProg2:
	case OpType::kIfFoodAhead:
		++nonterm_count;
		for (Node* child : children_) {
			child->CountNodes(term_count, nonterm_count);
		}
		break;
	case OpType::kMoveForward:
	case OpType::kTurnLeft:
	case OpType::kTurnRight:
		++term_count;
		break;
	}
}

bool Node::IsNonterminal() {
	switch (op_) {
	case OpType::kProg3:
	case OpType::kProg2:
	case OpType::kIfFoodAhead:
		return true;
	}
	return false;
}

bool Node::IsTerminal() {
	return !IsNonterminal();
}

void Node::CorrectNodes(Node *parent, size_t depth) {
	this->parent_ = parent;
	this->depth_ = depth;
	if (IsNonterminal()) {
		for (Node *child : children_) {
			child->CorrectNodes(this, depth + 1);
		}
	}
}

Node* Node::GetParent() {
	return parent_;
}
Node* Node::GetChild(size_t child_number) {
	if (child_number < children_.size()) {
		return children_[child_number];
	}
	return nullptr;
}
void Node::SetParent(Node *parent) {
	parent_ = parent;
}
void Node::SetChild(size_t child_number, Node *child) {
	if (child_number < children_.size()) {
		children_[child_number] = child;
	}
	children_.push_back(child);
}
size_t Node::GetCurrentDepth() {
	return depth_;
}
std::mt19937 &Node::GetEngine() {
	static std::random_device rd;
	static std::mt19937 mt(rd());
	return mt;
}<|endoftext|>


<commit_before>// This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
#ifndef __AIDA_HH__
#define __AIDA_HH__

#include "aidacc/aida.hh"
#include <assert.h>

// == Macros ==
#define TEST_CPP_PASTE2(a,b)    a ## b                          // Indirection helper, required to expand macros like __LINE__
#define TEST_CPP_PASTE(a,b)     TEST_CPP_PASTE2 (a,b)           ///< Paste two macro arguments into one C symbol name
#define TEST_CPP_STRINGIFY2(s)  #s                              // Indirection helper, required to expand macros like __LINE__
#define TEST_CPP_STRINGIFY(s)   TEST_CPP_STRINGIFY2 (s)         ///< Convert macro argument into a C const char*.
#define TASSERT(cond)           assert (cond)

// == TestChain ==
class TestChain {
  std::string           name_;
  std::function<void()> func_;
  const TestChain      *const next_;
  static TestChain*&    global_test_chain() { static TestChain *gchain = NULL; return gchain; }
public:
  static int run       (int argc, const char **argv);
  /*ctor*/   TestChain (std::function<void()> tfunc, const std::string &tname);
};
#define TEST_ADD(func)  static const ::TestChain TEST_CPP_PASTE (__TESTS_TestChain__, __LINE__) (func, TEST_CPP_STRINGIFY (func))

// == Implementations ==
TestChain::TestChain (std::function<void()> tfunc, const std::string &tname) :
  name_ (tname), func_ (tfunc), next_ (global_test_chain())
{
  AIDA_ASSERT_RETURN (next_ == global_test_chain());
  global_test_chain() = this;
}

int
TestChain::run (int argc, const char **argv)
{
  if (argc >= 1 && argv)
    printf ("  TEST     %s\n", argv[0]);
  for (const TestChain *t = global_test_chain(); t; t = t->next_)
    {
      fflush (stderr);
      printf ("  RUN…     %s\n", t->name_.c_str());
      fflush (stdout);
      t->func_();
      fflush (stderr);
      printf ("  PASS     %s\n", t->name_.c_str());
      fflush (stdout);
    }
  return 0;
}

#endif // __AIDA_HH__
<commit_msg>AIDACC: tests: testing.hh: add TCMP()<commit_after>// This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
#ifndef __AIDA_HH__
#define __AIDA_HH__

#include "aidacc/aida.hh"
#include <assert.h>

// == Macros ==
#define TEST_CPP_PASTE2(a,b)    a ## b                          // Indirection helper, required to expand macros like __LINE__
#define TEST_CPP_PASTE(a,b)     TEST_CPP_PASTE2 (a,b)           ///< Paste two macro arguments into one C symbol name
#define TEST_CPP_STRINGIFY2(s)  #s                              // Indirection helper, required to expand macros like __LINE__
#define TEST_CPP_STRINGIFY(s)   TEST_CPP_STRINGIFY2 (s)         ///< Convert macro argument into a C const char*.
#define TASSERT(cond)           assert (cond)
#define TCMP(a,op,b)            assert (a op b)

// == TestChain ==
class TestChain {
  std::string           name_;
  std::function<void()> func_;
  const TestChain      *const next_;
  static TestChain*&    global_test_chain() { static TestChain *gchain = NULL; return gchain; }
public:
  static int run       (int argc, const char **argv);
  /*ctor*/   TestChain (std::function<void()> tfunc, const std::string &tname);
};
#define TEST_ADD(func)  static const ::TestChain TEST_CPP_PASTE (__TESTS_TestChain__, __LINE__) (func, TEST_CPP_STRINGIFY (func))

// == Implementations ==
TestChain::TestChain (std::function<void()> tfunc, const std::string &tname) :
  name_ (tname), func_ (tfunc), next_ (global_test_chain())
{
  AIDA_ASSERT_RETURN (next_ == global_test_chain());
  global_test_chain() = this;
}

int
TestChain::run (int argc, const char **argv)
{
  if (argc >= 1 && argv)
    printf ("  TEST     %s\n", argv[0]);
  for (const TestChain *t = global_test_chain(); t; t = t->next_)
    {
      fflush (stderr);
      printf ("  RUN…     %s\n", t->name_.c_str());
      fflush (stdout);
      t->func_();
      fflush (stderr);
      printf ("  PASS     %s\n", t->name_.c_str());
      fflush (stdout);
    }
  return 0;
}

#endif // __AIDA_HH__
<|endoftext|>


<commit_before>#include <iostream>
#include <boost/filesystem.hpp>
#include <sys/uio.h>
#include <sstream>
#include <sys/types.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

bool is_number(const std::string& s)
{
    std::string::const_iterator it = s.begin();
    while (it != s.end() && std::isdigit(*it)) ++it;
    return !s.empty() && it == s.end();
}

using RC_Pointer = void*;

extern "C" RC_Pointer OpenRemoteProcess(RC_Pointer id, int desiredAccess)
{
	return id;
}

extern "C" bool IsProcessValid(RC_Pointer handle)
{
	return kill((pid_t)(intptr_t)handle, 0) == 0;
}

extern "C" bool ReadRemoteMemory(RC_Pointer handle, RC_Pointer address, RC_Pointer buffer, int offset, int size)
{
	iovec local[1];
	iovec remote[1];

	local[0].iov_base = ((uint8_t*)buffer + offset);
	local[0].iov_len = size;
	remote[0].iov_base = address;
	remote[0].iov_len = size;

	if (process_vm_readv((pid_t)(intptr_t)handle, local, 1, remote, 1, 0) != size)
	{
		return false;
	}

	return true;
}

extern "C" bool WriteRemoteMemory(RC_Pointer handle, RC_Pointer address, RC_Pointer buffer, int offset, int size)
{
	iovec local[1];
	iovec remote[1];

	local[0].iov_base = ((uint8_t*)buffer + offset);
	local[0].iov_len = size;
	remote[0].iov_base = address;
	remote[0].iov_len = size;

	if (process_vm_writev((pid_t)(intptr_t)handle, local, 1, remote, 1, 0) != size)
	{
		return false;
	}

	return true;
}

struct map_iterator
{
	off_t offset;
	int fd;
	size_t buf_size;
	char *buf;
	char *buf_end;
	char *path;
};

static inline char* ltoa(char *buf, long val)
{
	char *cp = buf, tmp;
	ssize_t i, len;

	do
	{
		*cp++ = '0' + (val % 10);
		val /= 10;
	} while (val);

	/* reverse the order of the digits: */
	len = cp - buf;
	--cp;
	for (i = 0; i < len / 2; ++i)
	{
		tmp = buf[i];
		buf[i] = cp[-i];
		cp[-i] = tmp;
	}
	return buf + len;
}

static inline int maps_init(struct map_iterator *mi, pid_t pid)
{
	char path[sizeof("/proc/0123456789/maps")], *cp;

	memcpy(path, "/proc/", 6);
	cp = ltoa(path + 6, pid);
	memcpy(cp, "/maps", 6);

	std::stringstream ss;
	ss << "/proc/" << pid << "/maps";

	mi->fd = open(ss.str().c_str(), O_RDONLY);
	if (mi->fd >= 0)
	{
		mi->buf_size = getpagesize();
		cp = (char*)mmap(nullptr, mi->buf_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
		if (cp == MAP_FAILED)
		{
			close(mi->fd);
			mi->fd = -1;
			return -1;
		}
		else
		{
			mi->offset = 0;
			mi->buf = mi->buf_end = cp + mi->buf_size;
			return 0;
		}
	}
	return -1;
}

static inline char* skip_whitespace(char *cp)
{
	if (!cp)
		return NULL;

	while (*cp == ' ' || *cp == '\t')
		++cp;
	return cp;
}

static inline char* scan_hex(char *cp, unsigned long *valp)
{
	unsigned long num_digits = 0, digit, val = 0;

	cp = skip_whitespace(cp);
	if (!cp)
		return NULL;

	while (1)
	{
		digit = *cp;
		if ((digit - '0') <= 9)
			digit -= '0';
		else if ((digit - 'a') < 6)
			digit -= 'a' - 10;
		else if ((digit - 'A') < 6)
			digit -= 'A' - 10;
		else
			break;
		val = (val << 4) | digit;
		++num_digits;
		++cp;
	}
	if (!num_digits)
		return NULL;
	*valp = val;
	return cp;
}

static inline char* scan_dec(char *cp, unsigned long *valp)
{
	unsigned long num_digits = 0, digit, val = 0;

	if (!(cp = skip_whitespace(cp)))
		return NULL;

	while (1)
	{
		digit = *cp;
		if ((digit - '0') <= 9)
		{
			digit -= '0';
			++cp;
		}
		else
			break;
		val = (10 * val) + digit;
		++num_digits;
	}
	if (!num_digits)
		return NULL;
	*valp = val;
	return cp;
}

static inline char* scan_char(char *cp, char *valp)
{
	if (!cp)
		return NULL;

	*valp = *cp;

	/* don't step over NUL terminator */
	if (*cp)
		++cp;
	return cp;
}

static inline char* scan_string(char *cp, char *valp, size_t buf_size)
{
	size_t i = 0;

	if (!(cp = skip_whitespace(cp)))
		return NULL;

	while (*cp != ' ' && *cp != '\t' && *cp != '\0')
	{
		if ((valp != NULL) && (i < buf_size - 1))
			valp[i++] = *cp;
		++cp;
	}
	if (i == 0 || i >= buf_size)
		return NULL;
	valp[i] = '\0';
	return cp;
}

static inline int maps_next(struct map_iterator *mi, unsigned long *low, unsigned long *high, unsigned long *offset)
{
	char perm[16], dash = 0, colon = 0, *cp;
	unsigned long major, minor, inum;
	ssize_t i, nread;

	if (mi->fd < 0)
		return 0;

	while (true)
	{
		ssize_t bytes_left = mi->buf_end - mi->buf;
		char *eol = NULL;

		for (i = 0; i < bytes_left; ++i)
		{
			if (mi->buf[i] == '\n')
			{
				eol = mi->buf + i;
				break;
			}
			else if (mi->buf[i] == '\0')
				break;
		}
		if (!eol)
		{
			/* copy down the remaining bytes, if any */
			if (bytes_left > 0)
				memmove(mi->buf_end - mi->buf_size, mi->buf, bytes_left);

			mi->buf = mi->buf_end - mi->buf_size;
			nread = read(mi->fd, mi->buf + bytes_left,
				mi->buf_size - bytes_left);
			if (nread <= 0)
				return 0;
			else if ((size_t)(nread + bytes_left) < mi->buf_size)
			{
				/* Move contents to the end of the buffer so we
				maintain the invariant that all bytes between
				mi->buf and mi->buf_end are valid.  */
				memmove(mi->buf_end - nread - bytes_left, mi->buf,
					nread + bytes_left);
				mi->buf = mi->buf_end - nread - bytes_left;
			}

			eol = mi->buf + bytes_left + nread - 1;

			for (i = bytes_left; i < bytes_left + nread; ++i)
				if (mi->buf[i] == '\n')
				{
					eol = mi->buf + i;
					break;
				}
		}
		cp = mi->buf;
		mi->buf = eol + 1;
		*eol = '\0';

		/* scan: "LOW-HIGH PERM OFFSET MAJOR:MINOR INUM PATH" */
		cp = scan_hex(cp, low);
		cp = scan_char(cp, &dash);
		cp = scan_hex(cp, high);
		cp = scan_string(cp, perm, sizeof(perm));
		cp = scan_hex(cp, offset);
		cp = scan_hex(cp, &major);
		cp = scan_char(cp, &colon);
		cp = scan_hex(cp, &minor);
		cp = scan_dec(cp, &inum);
		cp = mi->path = skip_whitespace(cp);
		if (!cp)
			continue;
		cp = scan_string(cp, NULL, 0);
		if (dash != '-' || colon != ':')
			continue;       /* skip line with unknown or bad format */
		return 1;
	}
	return 0;
}

static inline void maps_close(struct map_iterator *mi)
{
	if (mi->fd < 0)
		return;
	close(mi->fd);
	mi->fd = -1;
	if (mi->buf)
	{
		munmap(mi->buf_end - mi->buf_size, mi->buf_size);
		mi->buf = mi->buf_end = NULL;
	}
}

using EnumerateRemoteSectionData = void;
using EnumerateRemoteModuleData = void;

typedef void(EnumerateRemoteSectionsCallback)(EnumerateRemoteSectionData* data);
typedef void(EnumerateRemoteModulesCallback)(EnumerateRemoteModuleData* data);

extern "C" void EnumerateRemoteSectionsAndModules(RC_Pointer handle, EnumerateRemoteSectionsCallback callbackSection, EnumerateRemoteModulesCallback callbackModule)
{
	if (callbackSection == nullptr && callbackModule == nullptr)
	{
		return;
	}

	struct map_iterator mi;
	unsigned long start, end, offset, flags;
	struct map_info *map_list = NULL;
	struct map_info *cur_map;
	if (maps_init(&mi, (pid_t)(intptr_t)handle) < 0)
		return;
	while (maps_next(&mi, &start, &end, &offset, &flags))
	{
		cur_map = (struct map_info *)malloc(sizeof(struct map_info));
		if (cur_map == NULL)
			break;
		cur_map->next = map_list;
		cur_map->start = start;
		cur_map->end = end;
		cur_map->offset = offset;
		cur_map->flags = flags;
		cur_map->path = strdup(mi.path);
		cur_map->ei.size = 0;
		cur_map->ei.image = NULL;
		map_list = cur_map;
	}
	maps_close(&mi);
}

template<typename T>
T parse_type(const std::string& s)
{
    std::stringstream ss(s);

    T val;
    ss >> val;
    return val;
}

int main()
{
    using namespace boost::filesystem;

    path proc("/proc");

    for (auto& p : directory_iterator(proc))
    {
        if (is_directory(p))
        {
            auto name = p.path().filename().string();
            if (is_number(name))
            {
                size_t pid = parse_type<size_t>(name);

                auto pa = p.path() / "exe";

                if (is_symlink(symlink_status(pa)))
                {
                    try
                    {
                        auto e = read_symlink(pa);

                        std::cout << "(" << pid << ", " << e.string() << ")" << std::endl;
                    }
                    catch(...)
                    {
                        std::cout << "Exception: " << std::endl;
                    }
                }
            }
        }
    }

    return 0;
}
<commit_msg>Removed test file.<commit_after><|endoftext|>


<commit_before>/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

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.
==============================================================================*/

#include "tensorflow/compiler/xla/client/client.h"

#include <string>
#include <utility>

#include "tensorflow/compiler/xla/execution_options_util.h"
#include "tensorflow/compiler/xla/legacy_flags/debug_options_flags.h"
#include "tensorflow/compiler/xla/literal_util.h"
#include "tensorflow/compiler/xla/ptr_util.h"
#include "tensorflow/compiler/xla/status_macros.h"
#include "tensorflow/compiler/xla/types.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/protobuf.h"
#include "tensorflow/core/platform/types.h"

namespace xla {

Client::Client(ServiceInterface* stub) : stub_(stub) {}

Client::~Client() = default;

StatusOr<std::unique_ptr<Literal>> Client::Transfer(
    const GlobalData& data, const Shape* shape_with_layout) {
  TransferToClientRequest request;
  *request.mutable_data() = data.handle();
  if (shape_with_layout != nullptr) {
    *request.mutable_shape_with_layout() = *shape_with_layout;
  }
  TransferToClientResponse response;

  VLOG(1) << "making transfer request";
  VLOG(3) << "TransferToClientRequest: {" << request.DebugString() << "}";
  Status s = stub_->TransferToClient(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }
  VLOG(3) << "TransferToClientResponse: {" << response.DebugString() << "}";

  if (!response.has_literal()) {
    return FailedPrecondition(
        "server provided response without a literal in "
        "TransferToClient request");
  }
  return MakeUnique<Literal>(response.literal());
}

StatusOr<std::unique_ptr<GlobalData>> Client::TransferToServer(
    const Literal& literal, const DeviceHandle* device_handle) {
  TransferToServerRequest request;
  *request.mutable_literal() = literal.ToProto();
  if (device_handle) {
    *request.mutable_device_handle() = *device_handle;
  }
  TransferToServerResponse response;

  VLOG(1) << "making transfer to server request";
  VLOG(3) << "TransferToServerRequest: {" << request.DebugString() << "}";
  Status s = stub_->TransferToServer(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }
  VLOG(3) << "TransferToServerResponse: {" << response.DebugString() << "}";

  if (!response.has_data()) {
    return FailedPrecondition(
        "server provided response without a data handle in "
        "TransferToServer request");
  }

  return MakeUnique<GlobalData>(stub_, response.data());
}

Status Client::TransferToInfeed(const Literal& literal, int64 replica_id,
                                const DeviceHandle* device_handle) {
  TransferToInfeedRequest request;
  *request.mutable_literal() = literal.ToProto();
  if (device_handle) {
    *request.mutable_device_handle() = *device_handle;
  }
  request.set_replica_id(replica_id);
  TransferToInfeedResponse response;

  VLOG(1) << "making transfer to infeed request";
  VLOG(3) << "TransferToInfeedRequest: {" << request.DebugString() << "}";
  Status s = stub_->TransferToInfeed(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }
  VLOG(3) << "TransferToInfeedResponse: {" << response.DebugString() << "}";
  return Status::OK();
}

StatusOr<std::unique_ptr<Literal>> Client::TransferFromOutfeed(
    const Shape* shape_with_layout, int64 replica_id,
    const DeviceHandle* device_handle) {
  TransferFromOutfeedRequest request;
  if (device_handle) {
    *request.mutable_device_handle() = *device_handle;
  }
  request.set_replica_id(replica_id);
  if (shape_with_layout != nullptr) {
    *request.mutable_shape_with_layout() = *shape_with_layout;
  }
  TransferFromOutfeedResponse response;

  VLOG(1) << "making transfer from outfeed request";
  VLOG(3) << "TransferFromOutfeedRequest: {" << request.DebugString() << "}";
  Status s = stub_->TransferFromOutfeed(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }
  VLOG(3) << "TransferFromOutfeedResponse: {" << response.DebugString() << "}";

  if (!response.has_literal()) {
    return FailedPrecondition(
        "server provided response without a literal in "
        "TransferToClient request");
  }

  Literal literal(response.literal());
  return MakeUnique<Literal>(literal);
}

Status Client::ResetDevice() {
  ResetDeviceRequest request;
  ResetDeviceResponse response;

  VLOG(1) << "making reset device request";
  VLOG(3) << "ResetDeviceRequest: {" << request.DebugString() << "}";
  Status s = stub_->ResetDevice(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }
  VLOG(3) << "ResetDeviceResponse: {" << response.DebugString() << "}";
  return Status::OK();
}

StatusOr<std::unique_ptr<Literal>> Client::ExecuteAndTransfer(
    const Computation& computation,
    tensorflow::gtl::ArraySlice<GlobalData*> arguments,
    const ExecutionOptions* execution_options,
    ExecutionProfile* execution_profile) {
  TF_ASSIGN_OR_RETURN(
      std::unique_ptr<GlobalData> data,
      Execute(computation, arguments, execution_options, execution_profile));

  const Shape* shape_with_output_layout = nullptr;
  if (execution_options && execution_options->has_shape_with_output_layout()) {
    shape_with_output_layout = &execution_options->shape_with_output_layout();
  }
  return Transfer(*data, shape_with_output_layout);
}

StatusOr<Computation> Client::LoadSnapshot(const SessionModule& module) {
  LoadComputationSnapshotRequest request;
  *request.mutable_module() = module;
  LoadComputationSnapshotResponse response;

  Status s = stub_->LoadComputationSnapshot(&request, &response);
  if (!s.ok()) {
    return s;
  }

  VLOG(1) << "load snapshot response: " << response.ShortDebugString();
  return Computation(stub_, response.computation());
}

StatusOr<std::unique_ptr<GlobalData>> Client::Execute(
    const Computation& computation,
    tensorflow::gtl::ArraySlice<GlobalData*> arguments,
    const ExecutionOptions* execution_options,
    ExecutionProfile* execution_profile) {
  ExecuteRequest request;
  *request.mutable_computation() = computation.handle();

  if (execution_options == nullptr) {
    *request.mutable_execution_options() = CreateDefaultExecutionOptions();
  } else {
    *request.mutable_execution_options() = *execution_options;
  }
  for (GlobalData* argument : arguments) {
    CHECK(argument != nullptr) << "Argument pointers must not be null.";
    *request.add_arguments() = argument->handle();
  }

  ExecuteResponse response;
  VLOG(1) << "making execute request: " << request.ShortDebugString();
  Status s = stub_->Execute(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }

  if (execution_profile != nullptr) {
    *execution_profile = response.profile();
    if (VLOG_IS_ON(1)) {
      TF_ASSIGN_OR_RETURN(
          auto execution_stats,
          ExecutionStatsAsString(computation, response.profile()));
      VLOG(1) << execution_stats;
    }
  }

  return MakeUnique<GlobalData>(stub_, response.output());
}

StatusOr<std::vector<std::unique_ptr<GlobalData>>> Client::ExecuteParallel(
    tensorflow::gtl::ArraySlice<ComputationInstance> computations) {
  ExecuteParallelRequest request;

  for (const ComputationInstance& computation : computations) {
    ExecuteRequest single_request;
    *single_request.mutable_computation() = computation.computation.handle();
    for (GlobalData* argument : computation.arguments) {
      *single_request.add_arguments() = argument->handle();
    }
    *single_request.mutable_execution_options() = computation.execution_options;
    *request.add_requests() = single_request;
  }

  ExecuteParallelResponse response;
  VLOG(1) << "making execute-parallel request: " << request.ShortDebugString();
  tensorflow::Status s = stub_->ExecuteParallel(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }

  std::vector<std::unique_ptr<GlobalData>> outputs;
  for (size_t i = 0; i < computations.size(); ++i) {
    outputs.push_back(
        MakeUnique<GlobalData>(stub_, response.responses(i).output()));
    if (computations[i].execution_profile != nullptr) {
      *computations[i].execution_profile = response.responses(i).profile();
    }
  }

  return std::move(outputs);
}

StatusOr<std::vector<DeviceHandle>> Client::GetDeviceHandles(
    int64 device_count) {
  if (device_count < 1) {
    return InvalidArgument("device_count must be greater than 0");
  }
  GetDeviceHandlesRequest request;
  request.set_device_count(device_count);

  GetDeviceHandlesResponse response;
  VLOG(1) << "making get device request: " << request.ShortDebugString();
  tensorflow::Status s = stub_->GetDeviceHandles(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }

  std::vector<DeviceHandle> device_handles;
  for (const DeviceHandle& device_handle : response.device_handles()) {
    device_handles.push_back(device_handle);
  }

  return device_handles;
}

Status Client::Unregister(const GlobalData& data) {
  UnregisterRequest request;
  *request.mutable_data() = data.handle();
  UnregisterResponse response;

  VLOG(1) << "making unregister request";
  Status s = stub_->Unregister(&request, &response);
  VLOG(1) << "done with request";

  return s;
}

StatusOr<std::vector<std::unique_ptr<GlobalData>>> Client::DeconstructTuple(
    const GlobalData& data) {
  DeconstructTupleRequest request;
  *request.mutable_tuple_handle() = data.handle();
  DeconstructTupleResponse response;

  VLOG(1) << "making DestructTuple request";
  Status s = stub_->DeconstructTuple(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }

  std::vector<std::unique_ptr<GlobalData>> handles;
  for (auto& handle : response.element_handles()) {
    handles.push_back(MakeUnique<GlobalData>(stub_, handle));
  }
  return std::move(handles);
}

StatusOr<ComputationStats> Client::GetComputationStats(
    const Computation& computation, const DebugOptions& debug_options) const {
  ComputationStatsRequest request;
  *request.mutable_computation() = computation.handle();
  *request.mutable_debug_options() = debug_options;
  ComputationStatsResponse response;

  VLOG(1) << "making computation stats request";
  Status s = stub_->GetComputationStats(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }
  CHECK(response.has_stats());
  return response.stats();
}

StatusOr<std::unique_ptr<ProgramShape>> Client::GetComputationShape(
    const Computation& computation) {
  GetComputationShapeRequest request;
  *request.mutable_computation() = computation.handle();
  GetComputationShapeResponse response;

  VLOG(1) << "making get-computation-shape request";
  Status s = stub_->GetComputationShape(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }

  return WrapUnique(response.release_program_shape());
}

StatusOr<Shape> Client::GetShape(const GlobalData& data) {
  GetShapeRequest request;
  *request.mutable_data() = data.handle();
  GetShapeResponse response;

  VLOG(1) << "making get shape request";
  Status s = stub_->GetShape(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }

  return response.shape();
}

StatusOr<string> Client::ExecutionStatsAsString(
    const Computation& computation, const ExecutionProfile& profile) {
  TF_ASSIGN_OR_RETURN(
      auto computation_stats,
      GetComputationStats(computation,
                          legacy_flags::GetDebugOptionsFromFlags()));
  int64 total_flops =
      computation_stats.flop_count() + computation_stats.transcendental_count();
  if (profile.compute_time_ns() > 0) {
    int64 nanoseconds = profile.compute_time_ns();
    int64 cycle_count = profile.compute_cycle_count();
    double gflops = total_flops / nanoseconds;
    return tensorflow::strings::StrCat(
        "[Execution Statistics] flop count: ", computation_stats.flop_count(),
        ", transcendental count: ", computation_stats.transcendental_count(),
        ", compute execution time: ", nanoseconds, " nsec",
        ", compute cycles: ", cycle_count, ", performance: ", gflops,
        "gflop/s");
  }
  return string("[Execution Statistics] not available.");
}

StatusOr<ChannelHandle> Client::CreateChannelHandle() {
  CreateChannelHandleRequest request;
  CreateChannelHandleResponse response;

  VLOG(1) << "making create channel handle request";
  Status s = stub_->CreateChannelHandle(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }

  return response.channel();
}

}  // namespace xla
<commit_msg>Remove extra copy of literal in client TransferToOutfeed<commit_after>/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

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.
==============================================================================*/

#include "tensorflow/compiler/xla/client/client.h"

#include <string>
#include <utility>

#include "tensorflow/compiler/xla/execution_options_util.h"
#include "tensorflow/compiler/xla/legacy_flags/debug_options_flags.h"
#include "tensorflow/compiler/xla/literal_util.h"
#include "tensorflow/compiler/xla/ptr_util.h"
#include "tensorflow/compiler/xla/status_macros.h"
#include "tensorflow/compiler/xla/types.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/protobuf.h"
#include "tensorflow/core/platform/types.h"

namespace xla {

Client::Client(ServiceInterface* stub) : stub_(stub) {}

Client::~Client() = default;

StatusOr<std::unique_ptr<Literal>> Client::Transfer(
    const GlobalData& data, const Shape* shape_with_layout) {
  TransferToClientRequest request;
  *request.mutable_data() = data.handle();
  if (shape_with_layout != nullptr) {
    *request.mutable_shape_with_layout() = *shape_with_layout;
  }
  TransferToClientResponse response;

  VLOG(1) << "making transfer request";
  VLOG(3) << "TransferToClientRequest: {" << request.DebugString() << "}";
  Status s = stub_->TransferToClient(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }
  VLOG(3) << "TransferToClientResponse: {" << response.DebugString() << "}";

  if (!response.has_literal()) {
    return FailedPrecondition(
        "server provided response without a literal in "
        "TransferToClient request");
  }
  return MakeUnique<Literal>(response.literal());
}

StatusOr<std::unique_ptr<GlobalData>> Client::TransferToServer(
    const Literal& literal, const DeviceHandle* device_handle) {
  TransferToServerRequest request;
  *request.mutable_literal() = literal.ToProto();
  if (device_handle) {
    *request.mutable_device_handle() = *device_handle;
  }
  TransferToServerResponse response;

  VLOG(1) << "making transfer to server request";
  VLOG(3) << "TransferToServerRequest: {" << request.DebugString() << "}";
  Status s = stub_->TransferToServer(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }
  VLOG(3) << "TransferToServerResponse: {" << response.DebugString() << "}";

  if (!response.has_data()) {
    return FailedPrecondition(
        "server provided response without a data handle in "
        "TransferToServer request");
  }

  return MakeUnique<GlobalData>(stub_, response.data());
}

Status Client::TransferToInfeed(const Literal& literal, int64 replica_id,
                                const DeviceHandle* device_handle) {
  TransferToInfeedRequest request;
  *request.mutable_literal() = literal.ToProto();
  if (device_handle) {
    *request.mutable_device_handle() = *device_handle;
  }
  request.set_replica_id(replica_id);
  TransferToInfeedResponse response;

  VLOG(1) << "making transfer to infeed request";
  VLOG(3) << "TransferToInfeedRequest: {" << request.DebugString() << "}";
  Status s = stub_->TransferToInfeed(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }
  VLOG(3) << "TransferToInfeedResponse: {" << response.DebugString() << "}";
  return Status::OK();
}

StatusOr<std::unique_ptr<Literal>> Client::TransferFromOutfeed(
    const Shape* shape_with_layout, int64 replica_id,
    const DeviceHandle* device_handle) {
  TransferFromOutfeedRequest request;
  if (device_handle) {
    *request.mutable_device_handle() = *device_handle;
  }
  request.set_replica_id(replica_id);
  if (shape_with_layout != nullptr) {
    *request.mutable_shape_with_layout() = *shape_with_layout;
  }
  TransferFromOutfeedResponse response;

  VLOG(1) << "making transfer from outfeed request";
  VLOG(3) << "TransferFromOutfeedRequest: {" << request.DebugString() << "}";
  Status s = stub_->TransferFromOutfeed(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }
  VLOG(3) << "TransferFromOutfeedResponse: {" << response.DebugString() << "}";

  if (!response.has_literal()) {
    return FailedPrecondition(
        "server provided response without a literal in "
        "TransferToClient request");
  }

  return MakeUnique<Literal>(response.literal());
}

Status Client::ResetDevice() {
  ResetDeviceRequest request;
  ResetDeviceResponse response;

  VLOG(1) << "making reset device request";
  VLOG(3) << "ResetDeviceRequest: {" << request.DebugString() << "}";
  Status s = stub_->ResetDevice(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }
  VLOG(3) << "ResetDeviceResponse: {" << response.DebugString() << "}";
  return Status::OK();
}

StatusOr<std::unique_ptr<Literal>> Client::ExecuteAndTransfer(
    const Computation& computation,
    tensorflow::gtl::ArraySlice<GlobalData*> arguments,
    const ExecutionOptions* execution_options,
    ExecutionProfile* execution_profile) {
  TF_ASSIGN_OR_RETURN(
      std::unique_ptr<GlobalData> data,
      Execute(computation, arguments, execution_options, execution_profile));

  const Shape* shape_with_output_layout = nullptr;
  if (execution_options && execution_options->has_shape_with_output_layout()) {
    shape_with_output_layout = &execution_options->shape_with_output_layout();
  }
  return Transfer(*data, shape_with_output_layout);
}

StatusOr<Computation> Client::LoadSnapshot(const SessionModule& module) {
  LoadComputationSnapshotRequest request;
  *request.mutable_module() = module;
  LoadComputationSnapshotResponse response;

  Status s = stub_->LoadComputationSnapshot(&request, &response);
  if (!s.ok()) {
    return s;
  }

  VLOG(1) << "load snapshot response: " << response.ShortDebugString();
  return Computation(stub_, response.computation());
}

StatusOr<std::unique_ptr<GlobalData>> Client::Execute(
    const Computation& computation,
    tensorflow::gtl::ArraySlice<GlobalData*> arguments,
    const ExecutionOptions* execution_options,
    ExecutionProfile* execution_profile) {
  ExecuteRequest request;
  *request.mutable_computation() = computation.handle();

  if (execution_options == nullptr) {
    *request.mutable_execution_options() = CreateDefaultExecutionOptions();
  } else {
    *request.mutable_execution_options() = *execution_options;
  }
  for (GlobalData* argument : arguments) {
    CHECK(argument != nullptr) << "Argument pointers must not be null.";
    *request.add_arguments() = argument->handle();
  }

  ExecuteResponse response;
  VLOG(1) << "making execute request: " << request.ShortDebugString();
  Status s = stub_->Execute(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }

  if (execution_profile != nullptr) {
    *execution_profile = response.profile();
    if (VLOG_IS_ON(1)) {
      TF_ASSIGN_OR_RETURN(
          auto execution_stats,
          ExecutionStatsAsString(computation, response.profile()));
      VLOG(1) << execution_stats;
    }
  }

  return MakeUnique<GlobalData>(stub_, response.output());
}

StatusOr<std::vector<std::unique_ptr<GlobalData>>> Client::ExecuteParallel(
    tensorflow::gtl::ArraySlice<ComputationInstance> computations) {
  ExecuteParallelRequest request;

  for (const ComputationInstance& computation : computations) {
    ExecuteRequest single_request;
    *single_request.mutable_computation() = computation.computation.handle();
    for (GlobalData* argument : computation.arguments) {
      *single_request.add_arguments() = argument->handle();
    }
    *single_request.mutable_execution_options() = computation.execution_options;
    *request.add_requests() = single_request;
  }

  ExecuteParallelResponse response;
  VLOG(1) << "making execute-parallel request: " << request.ShortDebugString();
  tensorflow::Status s = stub_->ExecuteParallel(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }

  std::vector<std::unique_ptr<GlobalData>> outputs;
  for (size_t i = 0; i < computations.size(); ++i) {
    outputs.push_back(
        MakeUnique<GlobalData>(stub_, response.responses(i).output()));
    if (computations[i].execution_profile != nullptr) {
      *computations[i].execution_profile = response.responses(i).profile();
    }
  }

  return std::move(outputs);
}

StatusOr<std::vector<DeviceHandle>> Client::GetDeviceHandles(
    int64 device_count) {
  if (device_count < 1) {
    return InvalidArgument("device_count must be greater than 0");
  }
  GetDeviceHandlesRequest request;
  request.set_device_count(device_count);

  GetDeviceHandlesResponse response;
  VLOG(1) << "making get device request: " << request.ShortDebugString();
  tensorflow::Status s = stub_->GetDeviceHandles(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }

  std::vector<DeviceHandle> device_handles;
  for (const DeviceHandle& device_handle : response.device_handles()) {
    device_handles.push_back(device_handle);
  }

  return device_handles;
}

Status Client::Unregister(const GlobalData& data) {
  UnregisterRequest request;
  *request.mutable_data() = data.handle();
  UnregisterResponse response;

  VLOG(1) << "making unregister request";
  Status s = stub_->Unregister(&request, &response);
  VLOG(1) << "done with request";

  return s;
}

StatusOr<std::vector<std::unique_ptr<GlobalData>>> Client::DeconstructTuple(
    const GlobalData& data) {
  DeconstructTupleRequest request;
  *request.mutable_tuple_handle() = data.handle();
  DeconstructTupleResponse response;

  VLOG(1) << "making DestructTuple request";
  Status s = stub_->DeconstructTuple(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }

  std::vector<std::unique_ptr<GlobalData>> handles;
  for (auto& handle : response.element_handles()) {
    handles.push_back(MakeUnique<GlobalData>(stub_, handle));
  }
  return std::move(handles);
}

StatusOr<ComputationStats> Client::GetComputationStats(
    const Computation& computation, const DebugOptions& debug_options) const {
  ComputationStatsRequest request;
  *request.mutable_computation() = computation.handle();
  *request.mutable_debug_options() = debug_options;
  ComputationStatsResponse response;

  VLOG(1) << "making computation stats request";
  Status s = stub_->GetComputationStats(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }
  CHECK(response.has_stats());
  return response.stats();
}

StatusOr<std::unique_ptr<ProgramShape>> Client::GetComputationShape(
    const Computation& computation) {
  GetComputationShapeRequest request;
  *request.mutable_computation() = computation.handle();
  GetComputationShapeResponse response;

  VLOG(1) << "making get-computation-shape request";
  Status s = stub_->GetComputationShape(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }

  return WrapUnique(response.release_program_shape());
}

StatusOr<Shape> Client::GetShape(const GlobalData& data) {
  GetShapeRequest request;
  *request.mutable_data() = data.handle();
  GetShapeResponse response;

  VLOG(1) << "making get shape request";
  Status s = stub_->GetShape(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }

  return response.shape();
}

StatusOr<string> Client::ExecutionStatsAsString(
    const Computation& computation, const ExecutionProfile& profile) {
  TF_ASSIGN_OR_RETURN(
      auto computation_stats,
      GetComputationStats(computation,
                          legacy_flags::GetDebugOptionsFromFlags()));
  int64 total_flops =
      computation_stats.flop_count() + computation_stats.transcendental_count();
  if (profile.compute_time_ns() > 0) {
    int64 nanoseconds = profile.compute_time_ns();
    int64 cycle_count = profile.compute_cycle_count();
    double gflops = total_flops / nanoseconds;
    return tensorflow::strings::StrCat(
        "[Execution Statistics] flop count: ", computation_stats.flop_count(),
        ", transcendental count: ", computation_stats.transcendental_count(),
        ", compute execution time: ", nanoseconds, " nsec",
        ", compute cycles: ", cycle_count, ", performance: ", gflops,
        "gflop/s");
  }
  return string("[Execution Statistics] not available.");
}

StatusOr<ChannelHandle> Client::CreateChannelHandle() {
  CreateChannelHandleRequest request;
  CreateChannelHandleResponse response;

  VLOG(1) << "making create channel handle request";
  Status s = stub_->CreateChannelHandle(&request, &response);
  VLOG(1) << "done with request";

  if (!s.ok()) {
    return s;
  }

  return response.channel();
}

}  // namespace xla
<|endoftext|>


<commit_before>//===-- InMemoryAssemblerTest.cpp -------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "InMemoryAssembler.h"
#include "X86InstrInfo.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/MC/MCInstBuilder.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <memory>

namespace exegesis {
namespace {

using llvm::MCInstBuilder;
using llvm::X86::EAX;
using llvm::X86::MOV32ri;
using llvm::X86::MOV64ri32;
using llvm::X86::RAX;
using llvm::X86::XOR32rr;
using testing::ElementsAre;

class MachineFunctionGeneratorTest : public ::testing::Test {
protected:
  MachineFunctionGeneratorTest()
      : TT(llvm::sys::getProcessTriple()),
        CpuName(llvm::sys::getHostCPUName().str()) {}

  static void SetUpTestCase() {
    LLVMInitializeX86TargetInfo();
    LLVMInitializeX86TargetMC();
    LLVMInitializeX86Target();
    LLVMInitializeX86AsmPrinter();
  }

  std::unique_ptr<llvm::LLVMTargetMachine> createTargetMachine() {
    std::string Error;
    const llvm::Target *TheTarget =
        llvm::TargetRegistry::lookupTarget(TT, Error);
    EXPECT_TRUE(TheTarget) << Error << " " << TT;
    const llvm::TargetOptions Options;
    llvm::TargetMachine* TM = TheTarget->createTargetMachine(
            TT, CpuName, "", Options, llvm::Reloc::Model::Static);
    EXPECT_TRUE(TM) << TT << " " << CpuName;
    return std::unique_ptr<llvm::LLVMTargetMachine>(
        static_cast<llvm::LLVMTargetMachine *>(TM));
  }

  bool IsSupportedTarget() const {
    return llvm::StringRef(TT).startswith_lower("x86_64");
  }

private:
  const std::string TT;
  const std::string CpuName;
};

// Used to skip tests on unsupported architectures and operating systems.
// To skip a test, add this macro at the top of a test-case.
#define SKIP_UNSUPPORTED_PLATFORM \
  do \
    if (!IsSupportedTarget()) \
      return; \
  while(0)

TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunction) {
  SKIP_UNSUPPORTED_PLATFORM;
  JitFunctionContext Context(createTargetMachine());
  JitFunction Function(std::move(Context), {});
  ASSERT_THAT(Function.getFunctionBytes().str(), ElementsAre(0xc3));
  // FIXME: Check that the function runs without errors. Right now this is
  // disabled because it fails on some bots.
  // Function();
}

TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunctionXOR32rr) {
  SKIP_UNSUPPORTED_PLATFORM;
  JitFunctionContext Context(createTargetMachine());
  JitFunction Function(
      std::move(Context),
      {MCInstBuilder(XOR32rr).addReg(EAX).addReg(EAX).addReg(EAX)});
  ASSERT_THAT(Function.getFunctionBytes().str(), ElementsAre(0x31, 0xc0, 0xc3));
  // Function();
}

TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunctionMOV64ri) {
  SKIP_UNSUPPORTED_PLATFORM;
  JitFunctionContext Context(createTargetMachine());
  JitFunction Function(std::move(Context),
                       {MCInstBuilder(MOV64ri32).addReg(RAX).addImm(42)});
  ASSERT_THAT(Function.getFunctionBytes().str(),
              ElementsAre(0x48, 0xc7, 0xc0, 0x2a, 0x00, 0x00, 0x00, 0xc3));
  // Function();
}

TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunctionMOV32ri) {
  SKIP_UNSUPPORTED_PLATFORM;
  JitFunctionContext Context(createTargetMachine());
  JitFunction Function(std::move(Context),
                       {MCInstBuilder(MOV32ri).addReg(EAX).addImm(42)});
  ASSERT_THAT(Function.getFunctionBytes().str(),
              ElementsAre(0xb8, 0x2a, 0x00, 0x00, 0x00, 0xc3));
  // Function();
}

} // namespace
} // namespace exegesis
<commit_msg>[llvm-exegesis] re-enable failing tests after r330026.<commit_after>//===-- InMemoryAssemblerTest.cpp -------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "InMemoryAssembler.h"
#include "X86InstrInfo.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/MC/MCInstBuilder.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <memory>

namespace exegesis {
namespace {

using llvm::MCInstBuilder;
using llvm::X86::EAX;
using llvm::X86::MOV32ri;
using llvm::X86::MOV64ri32;
using llvm::X86::RAX;
using llvm::X86::XOR32rr;
using testing::ElementsAre;

class MachineFunctionGeneratorTest : public ::testing::Test {
protected:
  MachineFunctionGeneratorTest()
      : TT(llvm::sys::getProcessTriple()),
        CpuName(llvm::sys::getHostCPUName().str()) {}

  static void SetUpTestCase() {
    LLVMInitializeX86TargetInfo();
    LLVMInitializeX86TargetMC();
    LLVMInitializeX86Target();
    LLVMInitializeX86AsmPrinter();
  }

  std::unique_ptr<llvm::LLVMTargetMachine> createTargetMachine() {
    std::string Error;
    const llvm::Target *TheTarget =
        llvm::TargetRegistry::lookupTarget(TT, Error);
    EXPECT_TRUE(TheTarget) << Error << " " << TT;
    const llvm::TargetOptions Options;
    llvm::TargetMachine* TM = TheTarget->createTargetMachine(
            TT, CpuName, "", Options, llvm::Reloc::Model::Static);
    EXPECT_TRUE(TM) << TT << " " << CpuName;
    return std::unique_ptr<llvm::LLVMTargetMachine>(
        static_cast<llvm::LLVMTargetMachine *>(TM));
  }

  bool IsSupportedTarget() const {
    return llvm::StringRef(TT).startswith_lower("x86_64");
  }

private:
  const std::string TT;
  const std::string CpuName;
};

// Used to skip tests on unsupported architectures and operating systems.
// To skip a test, add this macro at the top of a test-case.
#define SKIP_UNSUPPORTED_PLATFORM \
  do \
    if (!IsSupportedTarget()) \
      return; \
  while(0)

TEST_F(MachineFunctionGeneratorTest, JitFunction) {
  SKIP_UNSUPPORTED_PLATFORM;
  JitFunctionContext Context(createTargetMachine());
  JitFunction Function(std::move(Context), {});
  ASSERT_THAT(Function.getFunctionBytes().str(), ElementsAre(0xc3));
  // FIXME: Check that the function runs without errors. Right now this is
  // disabled because it fails on some bots.
  // Function();
}

TEST_F(MachineFunctionGeneratorTest, JitFunctionXOR32rr) {
  SKIP_UNSUPPORTED_PLATFORM;
  JitFunctionContext Context(createTargetMachine());
  JitFunction Function(
      std::move(Context),
      {MCInstBuilder(XOR32rr).addReg(EAX).addReg(EAX).addReg(EAX)});
  ASSERT_THAT(Function.getFunctionBytes().str(), ElementsAre(0x31, 0xc0, 0xc3));
  // Function();
}

TEST_F(MachineFunctionGeneratorTest, JitFunctionMOV64ri) {
  SKIP_UNSUPPORTED_PLATFORM;
  JitFunctionContext Context(createTargetMachine());
  JitFunction Function(std::move(Context),
                       {MCInstBuilder(MOV64ri32).addReg(RAX).addImm(42)});
  ASSERT_THAT(Function.getFunctionBytes().str(),
              ElementsAre(0x48, 0xc7, 0xc0, 0x2a, 0x00, 0x00, 0x00, 0xc3));
  // Function();
}

TEST_F(MachineFunctionGeneratorTest, JitFunctionMOV32ri) {
  SKIP_UNSUPPORTED_PLATFORM;
  JitFunctionContext Context(createTargetMachine());
  JitFunction Function(std::move(Context),
                       {MCInstBuilder(MOV32ri).addReg(EAX).addImm(42)});
  ASSERT_THAT(Function.getFunctionBytes().str(),
              ElementsAre(0xb8, 0x2a, 0x00, 0x00, 0x00, 0xc3));
  // Function();
}

} // namespace
} // namespace exegesis
<|endoftext|>


<commit_before>#ifndef RECURSE_REQUEST_HPP
#define RECURSE_REQUEST_HPP

#include <QTcpSocket>
#include <QHash>

class Request {

public:

    QString data;
    QTcpSocket *socket;

    //!
    //! \brief header
    //! Http request headers, eg: header["content-type"] = "text/plain"
    //!
    QHash<QString, QString> header;

    //!
    //! \brief body_parsed
    //! Data to be filled by body parsing middleware
    //!
    QHash<QString, QVariant> body_parsed;

    //!
    //! \brief body
    //!
    QString body;

    //!
    //! \brief method
    //! Http method, eg: GET
    //!
    QString method;

    //!
    //! \brief protocol
    //! Http protocol, eg: HTTP
    //!
    QString protocol;

    //!
    //! \brief url
    //! Http request url, eg: /helloworld
    //!
    QString url;

    //!
    //! \brief length
    //! Http request Content-Length
    //!
    qint64 length = 0;

    //!
    //! \brief ip
    //! Client ip address
    //!
    QHostAddress ip;

    //!
    //! \brief cookies
    //! Http cookies in key/value form
    //!
    QHash<QString, QString> cookies;

    //!
    //! \brief hostname
    //! Http hostname from "Host" http header
    //!
    QString hostname;

    //!
    //! \brief get
    //! Return http request header
    //!
    //! \param key of the header
    //! \return header value
    //!
    QString get(const QString &key) { return header[key]; };

    //!
    //! \brief parse
    //! parse data from request
    //!
    //! \param request
    //! \return true on success, false otherwise, considered bad request
    //!
    bool parse(QString request);

private:
    QRegExp httpRx = QRegExp("^(?=[A-Z]).* \\/.* HTTP\\/[0-9]\\.[0-9]\\r\\n");
};

bool Request::parse(QString request)
{
    // chunked encoding
    this->data += request;

    // Save client ip address
    this->ip = this->socket->peerAddress();

    // if no header is present, just append all data to request.body
    if (!this->data.contains(httpRx)) {
        this->body.append(this->data);
        return true;
    }

    QStringList data_list = this->data.split("\r\n");
    bool is_body = false;

    for (int i = 0; i < data_list.size(); ++i) {
        if (is_body) {
            this->body.append(data_list.at(i));
            this->length += this->body.size();
            continue;
        }

        QStringList entity_item = data_list.at(i).split(":");

        if (entity_item.length() < 2 && entity_item.at(0).size() < 1 && !is_body) {
            is_body = true;
            continue;
        }
        else if (i == 0 && entity_item.length() < 2) {
            QStringList first_line = entity_item.at(0).split(" ");
            this->method = first_line.at(0);
            this->url = first_line.at(1).trimmed();
            this->protocol = first_line.at(2).trimmed();
            continue;
        }

        this->header[entity_item.at(0).toLower()] = entity_item.at(1).trimmed();
    }

    if (this->header.contains("host"))
        this->hostname = this->header["host"];

    qDebug() << "this->object populated: "
        << this->method << this->url << this->header << this->protocol << this->body
        << this->hostname << this->ip
        << this->length;

    // extract cookies
    // eg: USER_TOKEN=Yes;test=val
    if (this->header.contains("cookie")) {
        for(const QString &cookie : this->get("cookie").split(";")) {
            int split = cookie.trimmed().indexOf("=");
            if (split == -1)
                continue;

            QString key = cookie.left(split).trimmed();
            if (!key.size())
                continue;

            QString value = cookie.mid(split + 1).trimmed();

            this->cookies[key] = value;
        }

        qDebug() << "cookies:\n" << this->cookies << "\n";
    }

    return true;
}

#endif
<commit_msg>request: minor comment updates<commit_after>#ifndef RECURSE_REQUEST_HPP
#define RECURSE_REQUEST_HPP

#include <QTcpSocket>
#include <QHash>

class Request {

public:

    //!
    //! \brief data
    //! client request buffer data
    //!
    QString data;

    //!
    //! \brief socket
    //! underlying client socket
    //!
    QTcpSocket *socket;

    //!
    //! \brief header
    //! Http request headers, eg: header["content-type"] = "text/plain"
    //!
    QHash<QString, QString> header;

    //!
    //! \brief body_parsed
    //! Data to be filled by body parsing middleware
    //!
    QHash<QString, QVariant> body_parsed;

    //!
    //! \brief body
    //!
    QString body;

    //!
    //! \brief method
    //! Http method, eg: GET
    //!
    QString method;

    //!
    //! \brief protocol
    //! Http protocol, eg: HTTP
    //!
    QString protocol;

    //!
    //! \brief url
    //! Http request url, eg: /helloworld
    //!
    QString url;

    //!
    //! \brief length
    //! Http request Content-Length
    //!
    qint64 length = 0;

    //!
    //! \brief ip
    //! Client ip address
    //!
    QHostAddress ip;

    //!
    //! \brief cookies
    //! Http cookies in key/value form
    //!
    QHash<QString, QString> cookies;

    //!
    //! \brief hostname
    //! Http hostname from "Host" http header
    //!
    QString hostname;

    //!
    //! \brief get
    //! Return http request header
    //!
    //! \param key of the header
    //! \return header value
    //!
    QString get(const QString &key) { return header[key]; };

    //!
    //! \brief parse
    //! parse data from request
    //!
    //! \param request
    //! \return true on success, false otherwise, considered bad request
    //!
    bool parse(QString request);

private:
    QRegExp httpRx = QRegExp("^(?=[A-Z]).* \\/.* HTTP\\/[0-9]\\.[0-9]\\r\\n");
};

bool Request::parse(QString request)
{
    // buffer all data
    this->data += request;

    // Save client ip address
    this->ip = this->socket->peerAddress();

    // if no header is present, just append all data to request.body
    if (!this->data.contains(httpRx)) {
        this->body.append(this->data);
        return true;
    }

    QStringList data_list = this->data.split("\r\n");
    bool is_body = false;

    for (int i = 0; i < data_list.size(); ++i) {
        if (is_body) {
            this->body.append(data_list.at(i));
            this->length += this->body.size();
            continue;
        }

        QStringList entity_item = data_list.at(i).split(":");

        if (entity_item.length() < 2 && entity_item.at(0).size() < 1 && !is_body) {
            is_body = true;
            continue;
        }
        else if (i == 0 && entity_item.length() < 2) {
            QStringList first_line = entity_item.at(0).split(" ");
            this->method = first_line.at(0);
            this->url = first_line.at(1).trimmed();
            this->protocol = first_line.at(2).trimmed();
            continue;
        }

        this->header[entity_item.at(0).toLower()] = entity_item.at(1).trimmed();
    }

    if (this->header.contains("host"))
        this->hostname = this->header["host"];

    qDebug() << "this->object populated: "
        << this->method << this->url << this->header << this->protocol << this->body
        << this->hostname << this->ip
        << this->length;

    // extract cookies
    // eg: USER_TOKEN=Yes;test=val
    if (this->header.contains("cookie")) {
        for(const QString &cookie : this->get("cookie").split(";")) {
            int split = cookie.trimmed().indexOf("=");
            if (split == -1)
                continue;

            QString key = cookie.left(split).trimmed();
            if (!key.size())
                continue;

            QString value = cookie.mid(split + 1).trimmed();

            this->cookies[key] = value;
        }

        qDebug() << "cookies:\n" << this->cookies << "\n";
    }

    return true;
}

#endif
<|endoftext|>


<commit_before>/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.

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.
==============================================================================*/

#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/test.h"

namespace tensorflow {

TEST(Logging, Log) {
  LOG(INFO) << "Hello";
  LOG(INFO) << "Another log message";
  LOG(ERROR) << "Error message";
  VLOG(1) << "A VLOG message";
  VLOG(2) << "A higher VLOG message";
  DVLOG(1) << "A DVLOG message";
  DVLOG(2) << "A higher DVLOG message";
}

TEST(Logging, CheckChecks) {
  CHECK(true);
  CHECK(7 > 5);
  string a("abc");
  string b("xyz");
  CHECK_EQ(a, a);
  CHECK_NE(a, b);
  CHECK_EQ(3, 3);
  CHECK_NE(4, 3);
  CHECK_GT(4, 3);
  CHECK_GE(3, 3);
  CHECK_LT(2, 3);
  CHECK_LE(2, 3);

  DCHECK(true);
  DCHECK(7 > 5);
  DCHECK_EQ(a, a);
  DCHECK_NE(a, b);
  DCHECK_EQ(3, 3);
  DCHECK_NE(4, 3);
  DCHECK_GT(4, 3);
  DCHECK_GE(3, 3);
  DCHECK_LT(2, 3);
  DCHECK_LE(2, 3);
}

TEST(LoggingDeathTest, FailedChecks) {
  string a("abc");
  string b("xyz");
  const char* p_const = "hello there";
  const char* p_null_const = nullptr;
  char mybuf[10];
  char* p_non_const = mybuf;
  char* p_null = nullptr;
  CHECK_NOTNULL(p_const);
  CHECK_NOTNULL(p_non_const);

  ASSERT_DEATH(CHECK(false), "false");
  ASSERT_DEATH(CHECK(9 < 7), "9 < 7");
  ASSERT_DEATH(CHECK_EQ(a, b), "a == b");
  ASSERT_DEATH(CHECK_EQ(3, 4), "3 == 4");
  ASSERT_DEATH(CHECK_NE(3, 3), "3 != 3");
  ASSERT_DEATH(CHECK_GT(2, 3), "2 > 3");
  ASSERT_DEATH(CHECK_GE(2, 3), "2 >= 3");
  ASSERT_DEATH(CHECK_LT(3, 2), "3 < 2");
  ASSERT_DEATH(CHECK_LE(3, 2), "3 <= 2");
  ASSERT_DEATH(CHECK(false), "false");
  ASSERT_DEATH(printf("%s", CHECK_NOTNULL(p_null)), "Must be non NULL");
  ASSERT_DEATH(printf("%s", CHECK_NOTNULL(p_null_const)), "Must be non NULL");
#ifndef NDEBUG
  ASSERT_DEATH(DCHECK(9 < 7), "9 < 7");
  ASSERT_DEATH(DCHECK(9 < 7), "9 < 7");
  ASSERT_DEATH(DCHECK_EQ(a, b), "a == b");
  ASSERT_DEATH(DCHECK_EQ(3, 4), "3 == 4");
  ASSERT_DEATH(DCHECK_NE(3, 3), "3 != 3");
  ASSERT_DEATH(DCHECK_GT(2, 3), "2 > 3");
  ASSERT_DEATH(DCHECK_GE(2, 3), "2 >= 3");
  ASSERT_DEATH(DCHECK_LT(3, 2), "3 < 2");
  ASSERT_DEATH(DCHECK_LE(3, 2), "3 <= 2");
#endif
}

TEST(InternalLogString, Basic) {
  // Just make sure that this code compiles (we don't actually verify
  // the output)
  internal::LogString(__FILE__, __LINE__, INFO, "Hello there");
}

}  // namespace tensorflow

#if defined(__linux__) && !defined(PLATFORM_POSIX_ANDROID)

#define _GNU_SOURCE

#include <fcntl.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <vector>

namespace tensorflow {

class LogSinkTest : public ::testing::Test {
 protected:
  static const size_t kBufLen = 1024;

  void SetUp() override {
    ASSERT_EQ(pipe(out_pipe_), 0);

    auto flags = fcntl(out_pipe_[0], F_GETFL);
    flags |= O_NONBLOCK;
    fcntl(out_pipe_[0], F_SETFL, flags);

    saved_stderr_ = dup(STDERR_FILENO);
    ASSERT_NE(saved_stderr_, -1);

    ASSERT_NE(dup2(out_pipe_[1], STDERR_FILENO), -1);
    ASSERT_EQ(close(out_pipe_[1]), 0);

    auto sinks = TFGetLogSinks();
#ifndef NO_DEFAULT_LOGGER
    ASSERT_EQ(sinks.size(), 1);
#else
    ASSERT_EQ(sinks.size(), 0);
#endif

    for(auto sink : TFGetLogSinks()) {
      TFRemoveLogSink(sink);
    }

    TFAddLogSink(&default_sink_);
  }

  void TearDown() override {
    ASSERT_NE(dup2(saved_stderr_, STDERR_FILENO), -1);
    saved_stderr_ = out_pipe_[0] = out_pipe_[1] = -1;

    for(auto sink : TFGetLogSinks()) {
      TFRemoveLogSink(sink);
    }
#ifndef NO_DEFAULT_LOGGER
    TFAddLogSink(&default_sink_);
#endif
  }

  std::string GetStdErr() {
    fflush(stderr);

    ssize_t len = read(out_pipe_[0], (void*)buf_, kBufLen);

    std::string ret;
    if(len > 0) {
      ret.assign(buf_, buf_ + len);
    }

    return ret;
  }

  static std::vector<std::string> SplitLines(const std::string& str) {
    size_t cur = 0, next = 0;

    std::vector<std::string> lines;
    while(cur != std::string::npos) {
      next = str.find_first_of('\n', cur);
      if(next != std::string::npos) {
        if(next > cur) {
          lines.emplace_back(str.substr(cur, next - cur));
        }
        if(next + 1 == str.size()) {
          cur = std::string::npos;
        } else {
          cur = next + 1;
        }
      } else {
        lines.emplace_back(str.substr(cur));
        cur = next;
      }
    }

    return lines;
  }

 private:
  int saved_stderr_ = -1;
  int out_pipe_[2] = { -1, -1 };
  char buf_[kBufLen + 1] = { '/0' };
  static TFDefaultLogSink default_sink_;
};

TFDefaultLogSink LogSinkTest::default_sink_;

struct TestSink : public TFLogSink {
  std::stringstream ss;

  void Send(const TFLogEntry& entry) override {
    ss << entry.ToString() << std::endl;
  }
};

TEST_F(LogSinkTest, testLogSinks) {
  // First log into the default log sink
  LOG(INFO) << "Foo";
  LOG(INFO) << "Bar";

  auto lines = SplitLines(GetStdErr());
  ASSERT_EQ(lines.size(), 2);
  ASSERT_NE(lines[0].find_first_of("Foo"), std::string::npos);
  ASSERT_NE(lines[1].find_first_of("Bar"), std::string::npos);

  // Remove the default log sink
  auto sinks = TFGetLogSinks();
  ASSERT_EQ(sinks.size(), 1);

  TFRemoveLogSink(sinks[0]);
  sinks = TFGetLogSinks();
  ASSERT_EQ(sinks.size(), 0);

  LOG(INFO) << "Foo";
  ASSERT_TRUE(GetStdErr().empty());

  static TestSink sink;
  TFAddLogSink(&sink);
  sinks = TFGetLogSinks();
  ASSERT_EQ(sinks.size(), 1);

  ASSERT_EQ(sink.ss.str(), "Foo\n");

  LOG(INFO) << "Bar";
  ASSERT_EQ(sink.ss.str(), "Foo\nBar\n");
}

}  // namespace tensorflow

#endif // defined(__linux__) && !defined(PLATFORM_POSIX_ANDROID)
<commit_msg>Use absl::StrSplit instead of doing the splitting myself<commit_after>/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.

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.
==============================================================================*/

#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/test.h"

namespace tensorflow {

TEST(Logging, Log) {
  LOG(INFO) << "Hello";
  LOG(INFO) << "Another log message";
  LOG(ERROR) << "Error message";
  VLOG(1) << "A VLOG message";
  VLOG(2) << "A higher VLOG message";
  DVLOG(1) << "A DVLOG message";
  DVLOG(2) << "A higher DVLOG message";
}

TEST(Logging, CheckChecks) {
  CHECK(true);
  CHECK(7 > 5);
  string a("abc");
  string b("xyz");
  CHECK_EQ(a, a);
  CHECK_NE(a, b);
  CHECK_EQ(3, 3);
  CHECK_NE(4, 3);
  CHECK_GT(4, 3);
  CHECK_GE(3, 3);
  CHECK_LT(2, 3);
  CHECK_LE(2, 3);

  DCHECK(true);
  DCHECK(7 > 5);
  DCHECK_EQ(a, a);
  DCHECK_NE(a, b);
  DCHECK_EQ(3, 3);
  DCHECK_NE(4, 3);
  DCHECK_GT(4, 3);
  DCHECK_GE(3, 3);
  DCHECK_LT(2, 3);
  DCHECK_LE(2, 3);
}

TEST(LoggingDeathTest, FailedChecks) {
  string a("abc");
  string b("xyz");
  const char* p_const = "hello there";
  const char* p_null_const = nullptr;
  char mybuf[10];
  char* p_non_const = mybuf;
  char* p_null = nullptr;
  CHECK_NOTNULL(p_const);
  CHECK_NOTNULL(p_non_const);

  ASSERT_DEATH(CHECK(false), "false");
  ASSERT_DEATH(CHECK(9 < 7), "9 < 7");
  ASSERT_DEATH(CHECK_EQ(a, b), "a == b");
  ASSERT_DEATH(CHECK_EQ(3, 4), "3 == 4");
  ASSERT_DEATH(CHECK_NE(3, 3), "3 != 3");
  ASSERT_DEATH(CHECK_GT(2, 3), "2 > 3");
  ASSERT_DEATH(CHECK_GE(2, 3), "2 >= 3");
  ASSERT_DEATH(CHECK_LT(3, 2), "3 < 2");
  ASSERT_DEATH(CHECK_LE(3, 2), "3 <= 2");
  ASSERT_DEATH(CHECK(false), "false");
  ASSERT_DEATH(printf("%s", CHECK_NOTNULL(p_null)), "Must be non NULL");
  ASSERT_DEATH(printf("%s", CHECK_NOTNULL(p_null_const)), "Must be non NULL");
#ifndef NDEBUG
  ASSERT_DEATH(DCHECK(9 < 7), "9 < 7");
  ASSERT_DEATH(DCHECK(9 < 7), "9 < 7");
  ASSERT_DEATH(DCHECK_EQ(a, b), "a == b");
  ASSERT_DEATH(DCHECK_EQ(3, 4), "3 == 4");
  ASSERT_DEATH(DCHECK_NE(3, 3), "3 != 3");
  ASSERT_DEATH(DCHECK_GT(2, 3), "2 > 3");
  ASSERT_DEATH(DCHECK_GE(2, 3), "2 >= 3");
  ASSERT_DEATH(DCHECK_LT(3, 2), "3 < 2");
  ASSERT_DEATH(DCHECK_LE(3, 2), "3 <= 2");
#endif
}

TEST(InternalLogString, Basic) {
  // Just make sure that this code compiles (we don't actually verify
  // the output)
  internal::LogString(__FILE__, __LINE__, INFO, "Hello there");
}

}  // namespace tensorflow

#if defined(__linux__) && !defined(PLATFORM_POSIX_ANDROID)

#include "absl/strings/str_split.h"
#include <vector>
#include <sstream>

#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

namespace tensorflow {

class LogSinkTest : public ::testing::Test {
 protected:
  static const size_t kBufLen = 1024;

  void SetUp() override {
    ASSERT_EQ(pipe(out_pipe_), 0);

    auto flags = fcntl(out_pipe_[0], F_GETFL);
    flags |= O_NONBLOCK;
    fcntl(out_pipe_[0], F_SETFL, flags);

    saved_stderr_ = dup(STDERR_FILENO);
    ASSERT_NE(saved_stderr_, -1);

    ASSERT_NE(dup2(out_pipe_[1], STDERR_FILENO), -1);
    ASSERT_EQ(close(out_pipe_[1]), 0);

    auto sinks = TFGetLogSinks();
#ifndef NO_DEFAULT_LOGGER
    ASSERT_EQ(sinks.size(), 1);
#else
    ASSERT_EQ(sinks.size(), 0);
#endif

    for(auto sink : TFGetLogSinks()) {
      TFRemoveLogSink(sink);
    }

    TFAddLogSink(&default_sink_);
  }

  void TearDown() override {
    ASSERT_NE(dup2(saved_stderr_, STDERR_FILENO), -1);
    saved_stderr_ = out_pipe_[0] = out_pipe_[1] = -1;

    for(auto sink : TFGetLogSinks()) {
      TFRemoveLogSink(sink);
    }
#ifndef NO_DEFAULT_LOGGER
    TFAddLogSink(&default_sink_);
#endif
  }

  std::string GetStdErr() {
    fflush(stderr);

    ssize_t len = read(out_pipe_[0], (void*)buf_, kBufLen);

    std::string ret;
    if(len > 0) {
      ret.assign(buf_, buf_ + len);
    }

    return ret;
  }

 private:
  int saved_stderr_ = -1;
  int out_pipe_[2] = { -1, -1 };
  char buf_[kBufLen + 1] = { '/0' };
  static TFDefaultLogSink default_sink_;
};

TFDefaultLogSink LogSinkTest::default_sink_;

struct TestSink : public TFLogSink {
  std::stringstream ss;

  void Send(const TFLogEntry& entry) override {
    ss << entry.ToString() << std::endl;
  }
};

TEST_F(LogSinkTest, testLogSinks) {
  // First log into the default log sink
  LOG(INFO) << "Foo";
  LOG(INFO) << "Bar";

  std::vector<std::string> lines = absl::StrSplit(GetStdErr(), '\n');
  ASSERT_EQ(lines.size(), 3);
  ASSERT_NE(lines[0].find_first_of("Foo"), std::string::npos);
  ASSERT_NE(lines[1].find_first_of("Bar"), std::string::npos);
  ASSERT_TRUE(lines[2].empty());

  // Remove the default log sink
  auto sinks = TFGetLogSinks();
  ASSERT_EQ(sinks.size(), 1);

  TFRemoveLogSink(sinks[0]);
  sinks = TFGetLogSinks();
  ASSERT_EQ(sinks.size(), 0);

  LOG(INFO) << "Foo";
  ASSERT_TRUE(GetStdErr().empty());

  static TestSink sink;
  TFAddLogSink(&sink);
  sinks = TFGetLogSinks();
  ASSERT_EQ(sinks.size(), 1);

  ASSERT_EQ(sink.ss.str(), "Foo\n");

  LOG(INFO) << "Bar";
  ASSERT_EQ(sink.ss.str(), "Foo\nBar\n");
}

}  // namespace tensorflow

#endif // defined(__linux__) && !defined(PLATFORM_POSIX_ANDROID)
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: itemdef.hxx,v $
 *
 *  $Revision: 1.2 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-09 09:23:16 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/
#ifndef ITEMDEF_HXX
#define ITEMDEF_HXX

#ifndef _SFXMSG_HXX //autogen
#include <sfx2/msg.hxx>
#endif

SFX_DECL_TYPE(10); //SwElemItem
SFX_DECL_TYPE(13); //SwAddPrinterItem
SFX_DECL_TYPE(16); //SwDocDisplayItem

#endif
<commit_msg>INTEGRATION: CWS changefileheader (1.2.1202); FILE MERGED 2008/04/01 15:59:14 thb 1.2.1202.2: #i85898# Stripping all external header guards 2008/03/31 16:58:33 rt 1.2.1202.1: #i87441# Change license header to LPGL v3.<commit_after>/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: itemdef.hxx,v $
 * $Revision: 1.3 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
#ifndef ITEMDEF_HXX
#define ITEMDEF_HXX

#include <sfx2/msg.hxx>

SFX_DECL_TYPE(10); //SwElemItem
SFX_DECL_TYPE(13); //SwAddPrinterItem
SFX_DECL_TYPE(16); //SwDocDisplayItem

#endif
<|endoftext|>