Beispiel #1
0
Initializer::Initializer(int& argc, char**& argv, ToolType tool)
    : argc_(&argc),
      argv_(&argv),
      tool_(tool),
      binary_(fs::path(std::string(argv[0])).filename().string()) {
  std::srand(time(nullptr));

  // osquery implements a custom help/usage output.
  for (int i = 1; i < *argc_; i++) {
    auto help = std::string((*argv_)[i]);
    if ((help == "--help" || help == "-help" || help == "--h" ||
         help == "-h") &&
        tool != OSQUERY_TOOL_TEST) {
      printUsage(binary_, tool_);
      ::exit(0);
    }
  }

// To change the default config plugin, compile osquery with
// -DOSQUERY_DEFAULT_CONFIG_PLUGIN=<new_default_plugin>
#ifdef OSQUERY_DEFAULT_CONFIG_PLUGIN
  FLAGS_config_plugin = STR(OSQUERY_DEFAULT_CONFIG_PLUGIN);
#endif

// To change the default logger plugin, compile osquery with
// -DOSQUERY_DEFAULT_LOGGER_PLUGIN=<new_default_plugin>
#ifdef OSQUERY_DEFAULT_LOGGER_PLUGIN
  FLAGS_logger_plugin = STR(OSQUERY_DEFAULT_LOGGER_PLUGIN);
#endif

  // Set version string from CMake build
  GFLAGS_NAMESPACE::SetVersionString(OSQUERY_VERSION);

  // Let gflags parse the non-help options/flags.
  GFLAGS_NAMESPACE::ParseCommandLineFlags(
      argc_, argv_, (tool == OSQUERY_TOOL_SHELL));

  if (tool == OSQUERY_TOOL_SHELL) {
    // The shell is transient, rewrite config-loaded paths.
    FLAGS_disable_logging = true;
    // Get the caller's home dir for temporary storage/state management.
    auto homedir = osqueryHomeDirectory();
    if (osquery::pathExists(homedir).ok() ||
        boost::filesystem::create_directory(homedir)) {
      osquery::FLAGS_database_path = homedir + "/shell.db";
      osquery::FLAGS_extensions_socket = homedir + "/shell.em";
    }
  }

  // If the caller is checking configuration, disable the watchdog/worker.
  if (FLAGS_config_check) {
    FLAGS_disable_watchdog = true;
  }

  // Initialize the status and results logger.
  initStatusLogger(binary_);
  if (tool != OSQUERY_EXTENSION) {
    if (isWorker()) {
      VLOG(1) << "osquery worker initialized [watcher="
              << getenv("OSQUERY_WORKER") << "]";
    } else {
      VLOG(1) << "osquery initialized [version=" << OSQUERY_VERSION << "]";
    }
  } else {
    VLOG(1) << "osquery extension initialized [sdk=" << OSQUERY_SDK_VERSION
            << "]";
  }
}
void BlenderSession::render()
{
	/* set callback to write out render results */
	session->write_render_tile_cb = function_bind(&BlenderSession::write_render_tile, this, _1);
	session->update_render_tile_cb = function_bind(&BlenderSession::update_render_tile, this, _1);

	/* get buffer parameters */
	SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
	BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);

	/* render each layer */
	BL::RenderSettings r = b_scene.render();
	BL::RenderSettings::layers_iterator b_layer_iter;
	BL::RenderResult::views_iterator b_view_iter;
	
	for(r.layers.begin(b_layer_iter); b_layer_iter != r.layers.end(); ++b_layer_iter) {
		b_rlay_name = b_layer_iter->name();

		/* temporary render result to find needed passes and views */
		BL::RenderResult b_rr = begin_render_result(b_engine, 0, 0, 1, 1, b_rlay_name.c_str(), NULL);
		BL::RenderResult::layers_iterator b_single_rlay;
		b_rr.layers.begin(b_single_rlay);

		/* layer will be missing if it was disabled in the UI */
		if(b_single_rlay == b_rr.layers.end()) {
			end_render_result(b_engine, b_rr, true, false);
			continue;
		}

		BL::RenderLayer b_rlay = *b_single_rlay;

		/* add passes */
		vector<Pass> passes;
		Pass::add(PASS_COMBINED, passes);
#ifdef WITH_CYCLES_DEBUG
		Pass::add(PASS_BVH_TRAVERSAL_STEPS, passes);
		/* Pass::add(PASS_RAY_BOUNCES, passes); */
#endif

		if(session_params.device.advanced_shading) {

			/* loop over passes */
			BL::RenderLayer::passes_iterator b_pass_iter;

			for(b_rlay.passes.begin(b_pass_iter); b_pass_iter != b_rlay.passes.end(); ++b_pass_iter) {
				BL::RenderPass b_pass(*b_pass_iter);
				PassType pass_type = get_pass_type(b_pass);

				if(pass_type == PASS_MOTION && scene->integrator->motion_blur)
					continue;
				if(pass_type != PASS_NONE)
					Pass::add(pass_type, passes);
			}
		}

		buffer_params.passes = passes;
		scene->film->pass_alpha_threshold = b_layer_iter->pass_alpha_threshold();
		scene->film->tag_passes_update(scene, passes);
		scene->film->tag_update(scene);
		scene->integrator->tag_update(scene);

		for(b_rr.views.begin(b_view_iter); b_view_iter != b_rr.views.end(); ++b_view_iter) {
			b_rview_name = b_view_iter->name();

			/* set the current view */
			b_engine.active_view_set(b_rview_name.c_str());

			/* update scene */
			sync->sync_camera(b_render, b_engine.camera_override(), width, height);
			sync->sync_data(b_v3d, b_engine.camera_override(), &python_thread_state, b_rlay_name.c_str());

			/* update number of samples per layer */
			int samples = sync->get_layer_samples();
			bool bound_samples = sync->get_layer_bound_samples();

			if(samples != 0 && (!bound_samples || (samples < session_params.samples)))
				session->reset(buffer_params, samples);
			else
				session->reset(buffer_params, session_params.samples);

			/* render */
			session->start();
			session->wait();

			if(session->progress.get_cancel())
				break;
		}

		/* free result without merging */
		end_render_result(b_engine, b_rr, true, false);

		if(session->progress.get_cancel())
			break;
	}

	double total_time, render_time;
	session->progress.get_time(total_time, render_time);
	VLOG(1) << "Total render time: " << total_time;
	VLOG(1) << "Render time (without synchronization): " << render_time;

	/* clear callback */
	session->write_render_tile_cb = function_null;
	session->update_render_tile_cb = function_null;

	/* free all memory used (host and device), so we wouldn't leave render
	 * engine with extra memory allocated
	 */

	session->device_free();

	delete sync;
	sync = NULL;
}
Beispiel #3
0
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Takes a clang::CastExpr, converts its subExpr into IR and wraps it with the necessary IR casts
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	core::ExpressionPtr performClangCastOnIR(insieme::frontend::conversion::Converter& converter, const clang::CastExpr* castExpr) {
		core::ExpressionPtr expr = converter.convertExpr(castExpr->getSubExpr());
		core::TypePtr targetTy = converter.convertType(castExpr->getType());
		core::TypePtr exprTy = expr->getType();

		if(VLOG_IS_ON(2)) {
			VLOG(2) << "castExpr: ";
			castExpr->dump();
			VLOG(2) << "\n";
		}

		const core::FrontendIRBuilder& builder = converter.getIRBuilder();
		//const core::lang::BasicGenerator& basic = builder.getLangBasic();
		//core::NodeManager& mgr = converter.getNodeManager();
		
		switch(castExpr->getCastKind()) {
		////////////////////////////////////////////////////////////////////////////////////////////////////////////	
		// A conversion which causes the extraction of an r-value from the operand gl-value.
		// The result of an r-value conversion is always unqualified.
		// IR: this is the same as ref_deref: ref<a'> -> a'
		case clang::CK_LValueToRValue:
			return builder.deref(expr);

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Numerical value type conversions
		// handled by IR numeric_cast
		case clang::CK_IntegralCast:
		case clang::CK_IntegralToFloating:
		case clang::CK_FloatingToIntegral:
		case clang::CK_FloatingCast:
			return builder.numericCast(expr, targetTy); 

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Numeric and pointer to boolean
		case clang::CK_IntegralToBoolean:
		case clang::CK_FloatingToBoolean:
		case clang::CK_PointerToBoolean:
			return utils::exprToBool(expr);

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// A conversion which causes a bit pattern of one type to be reinterpreted as a bit pattern of another type.
		// Generally the operands must have equivalent size and unrelated types.  The pointer conversion
		// char* -> int* is a bitcast. A conversion from any pointer type to a C pointer type is a bitcast unless
		// it's actually BaseToDerived or DerivedToBase. A conversion to a block pointer or ObjC pointer type is a
		// bitcast only if the operand has the same type kind; otherwise, it's one of the specialized casts below.
		// Vector coercions are bitcasts.
		case clang::CK_BitCast:
			return implementBitcast(converter, targetTy, expr);

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Integral to pointer. A special kind of reinterpreting conversion.
		// (char*) 0x1001aab0,  reinterpret_cast<int*>(0)
		case clang::CK_IntegralToPointer:
			return core::lang::buildPtrFromIntegral(expr, targetTy);

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Pointer to integral. A special kind of reinterpreting conversion.
		// (int)((void*)0x1001aab0)
		case clang::CK_PointerToIntegral:
			return core::lang::buildPtrToIntegral(expr, targetTy);

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Null pointer constant to pointer, e.g. (int*)0
		case clang::CK_NullToPointer:
			return core::lang::buildPtrNull(targetTy);

		//////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Array to pointer decay. int[10] -> int* char[5][6] -> char(*)[6]
		case clang::CK_ArrayToPointerDecay:
			return core::lang::buildPtrFromArray(expr);

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// CK_FunctionToPointerDecay - Function to pointer decay. void(int) -> void(*)(int)
		// CK_BuiltinFnToFnPtr - Same as above, for builtin functions
		case clang::CK_FunctionToPointerDecay:
		case clang::CK_BuiltinFnToFnPtr:
			return core::lang::buildPtrOfFunction(expr);

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// NoOps: Conversions that have no effect
		// * same type casts, CK_NoOp, e.g. int -> int
		case clang::CK_NoOp:
			return expr;
			
		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Unused return value: (void)fun()
		case clang::CK_ToVoid:
			return builder.unitConsume(expr);

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//case clang::CK_ConstructorConversion:
		//	// Conversion by constructor. struct A { A(int); }; A a = A(10);
		//	{
		//		// this should be handled by backend compiler
		//		// http://stackoverflow.com/questions/1384007/conversion-constructor-vs-conversion-operator-precedence
		//		return expr;
		//	}

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//case clang::CK_FloatingRealToComplex:
		//case clang::CK_IntegralRealToComplex:
		//	return builder.callExpr(mgr.getLangExtension<core::lang::ComplexExtension>().getConstantToComplex(), expr);
		///*A conversion of a floating point real to a floating point complex of the original type. Injects the value as the
		//* real component with a zero imaginary component. float -> _Complex float.
		//* */
		///*Converts from an integral real to an integral complex whose element type matches the source. Injects the value as the
		//* real component with a zero imaginary component. long -> _Complex long.
		//* */

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//case clang::CK_FloatingComplexCast:
		//case clang::CK_FloatingComplexToIntegralComplex:
		//case clang::CK_IntegralComplexCast:
		//case clang::CK_IntegralComplexToFloatingComplex:
		//	return mgr.getLangExtension<core::lang::ComplexExtension>().castComplexToComplex(expr, targetTy);
		///*Converts between different floating point complex types. _Complex float -> _Complex double.
		//* */
		///*Converts from a floating complex to an integral complex. _Complex float -> _Complex int.
		//* */
		///*Converts between different integral complex types. _Complex char -> _Complex long long _Complex unsigned int ->
		//* _Complex signed int.
		//* */
		///*Converts from an integral complex to a floating complex. _Complex unsigned -> _Complex float.
		//* */

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//case clang::CK_FloatingComplexToReal:
		//case clang::CK_IntegralComplexToReal:
		//	return mgr.getLangExtension<core::lang::ComplexExtension>().getReal(expr);
		///*Converts a floating point complex to floating point real of the source's element type. Just discards the imaginary
		//* component. _Complex long double -> long double.
		//* */
		///*Converts an integral complex to an integral real of the source's element type by discarding the imaginary component.
		//* _Complex short -> short.
		//* */


		///////////////////////////////////////////////////////////////////////////////////////////////////////////
		//case clang::CK_IntegralComplexToBoolean:
		//case clang::CK_FloatingComplexToBoolean:
		//	/*Converts a complex to bool by comparing against 0+0i.
		//	* */
		//	return mgr.getLangExtension<core::lang::ComplexExtension>().castComplexToBool(expr);

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//case clang::CK_LValueBitCast:
		//	/* case clang::CK_LValueBitCast - A conversion which reinterprets the address of an l-value as an l-value of a different
		//	* kind. Used for reinterpret_casts of l-value expressions to reference types. bool b; reinterpret_cast<char&>(b) = 'a';
		//	* */
		//	{
		//		// if we have a cpp ref we have to unwrap the expression
		//		if(core::analysis::isAnyCppRef(expr->getType())) { expr = builder.toIRRef(expr); }
		//		// the target type is a ref type because lvalue
		//		// bitcasts look like reinterpret_cast<type&>(x)
		//		targetTy = builder.refType(targetTy);
		//		core::CallExprPtr newExpr = builder.callExpr(targetTy, gen.getRefReinterpret(), expr, builder.getTypeLiteral(GET_REF_ELEM_TYPE(targetTy)));
		//		// wrap it as cpp ref
		//		return builder.callExpr(mgr.getLangExtension<core::lang::IRppExtensions>().getRefIRToCpp(), newExpr);
		//	}

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//case clang::CK_MemberPointerToBoolean:
		//	/*case clang::CK_MemberPointerToBoolean - Member pointer to boolean. A check against the null member pointer.
		//	* */
		//	{
		//		if(expr->getType().isa<core::FunctionTypePtr>()) {
		//			return builder.callExpr(gen.getBoolLNot(), builder.callExpr(gen.getBool(), gen.getFuncIsNull(), expr));
		//		}
		//		frontend_assert(core::analysis::isMemberPointer(expr->getType())) << " not a memberPointer? " << expr << " : " << expr->getType();

		//		return core::analysis::getMemberPointerCheck(expr);
		//	}

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		////  PARTIALY IMPLEMENTED
		////////////////////////////////////////////////////////////////////////////////////////////////////////////

		///////////////////////////////////////////////////////////////////////////////////////////////////////////
		//case clang::CK_UncheckedDerivedToBase:
		//case clang::CK_DerivedToBase:
		//	// A conversion from a C++ class pointer to a base class pointer. A *a = new B();
		//	{
		//		// TODO: do we need to check if is pointerType?
		//		// in case of pointer, the inner expression is modeled as ref< array < C, 1> >
		//		// it is needed to deref the first element
		//		expr = getCArrayElemRef(builder, expr);

		//		// unwrap CppRef if CppRef
		//		if(core::analysis::isAnyCppRef(exprTy)) { expr = core::analysis::unwrapCppRef(expr); }

		//		clang::CastExpr::path_const_iterator it;
		//		for(it = castExpr->path_begin(); it != castExpr->path_end(); ++it) {
		//			core::TypePtr targetTy = converter.convertType((*it)->getType());
		//			// if it is no ref we have to materialize it, otherwise refParent cannot be called
		//			if(expr->getType()->getNodeType() != core::NT_RefType) {
		//				// expr = builder.callExpr (mgr.getLangExtension<core::lang::IRppExtensions>().getMaterialize(), expr);
		//				expr = builder.refVar(expr);
		//			}
		//			expr = builder.refParent(expr, targetTy);
		//		}

		//		if(castExpr->getType().getTypePtr()->isPointerType()) {
		//			// is a pointer type -> return pointer
		//			expr = builder.callExpr(gen.getScalarToArray(), expr);
		//		}

		//		VLOG(2) << "cast resoult: \n" << dumpPretty(expr) << " \n of type: " << expr->getType();
		//		return expr;
		//	}

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//case clang::CK_BaseToDerived:
		//	// A conversion from a C++ class pointer/reference to a derived class pointer/reference. B *b = static_cast<B*>(a);
		//	{
		//		// we want to know the TYPE of static_cast<TYPE>()
		//		targetTy = converter.convertType(llvm::dyn_cast<clang::ExplicitCastExpr>(castExpr)->getType());
		//		VLOG(2) << exprTy << " " << targetTy;

		//		core::ExpressionPtr retIr;

		//		// pointers:
		//		if(core::analysis::isPointerType(exprTy)) {
		//			assert_true(core::analysis::isPointerType(targetTy)) << "from pointer to non pointer is not possible";
		//			targetTy = targetTy.as<core::RefTypePtr>()->getElementType();
		//			return builder.callExpr(mgr.getLangExtension<core::lang::IRppExtensions>().getStaticCast(), expr, builder.getTypeLiteral((targetTy)));
		//		}

		//		// NORE: All value casts are upgraded to CPP ref, this has no implications for the generated code since lvalues in clang are already refs.
		//		// 		 and makes everithing smoother and easyer
		//		if(exprTy.isa<core::RefTypePtr>()) {
		//			expr = builder.toCppRef(expr);
		//			exprTy = expr.getType();
		//		}

		//		if(core::analysis::isCppRef(exprTy)) {
		//			if(!core::analysis::isCppRef(targetTy)) { targetTy = core::analysis::getCppRef(targetTy); }
		//			retIr = builder.callExpr(mgr.getLangExtension<core::lang::IRppExtensions>().getStaticCastRefCppToRefCpp(), expr,
		//			                         builder.getTypeLiteral((targetTy)));
		//		} else if(core::analysis::isConstCppRef(exprTy)) {
		//			if(!core::analysis::isCppRef(targetTy)) { targetTy = core::analysis::getConstCppRef(targetTy); }
		//			retIr = builder.callExpr(mgr.getLangExtension<core::lang::IRppExtensions>().getStaticCastConstCppToConstCpp(), expr,
		//			                         builder.getTypeLiteral((targetTy)));
		//		} else {
		//			std::cerr << " === BASE TO DERIVED FAILED ===========" << std::endl;
		//			std::cerr << "####### Expr: #######" << std::endl;
		//			std::cerr << (expr);
		//			std::cerr << "\n####### Expr Type: #######" << std::endl;
		//			std::cerr << (exprTy);
		//			std::cerr << "\n####### cast Type: #######" << std::endl;
		//			std::cerr << (targetTy);
		//			std::cerr << "\n####### clang: #######" << std::endl;
		//			castExpr->dump();
		//			abort();
		//		}

		//		return retIr;
		//	}

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//case clang::CK_Dynamic:
		//	// A C++ dynamic_cast.
		//	{
		//		// we want to know the TYPE of static_cast<TYPE>()
		//		targetTy = converter.convertType(llvm::dyn_cast<clang::ExplicitCastExpr>(castExpr)->getType());
		//		VLOG(2) << exprTy << " " << targetTy;

		//		core::ExpressionPtr retIr;

		//		// pointers:
		//		if(core::analysis::isPointerType(exprTy)) {
		//			assert_true(core::analysis::isPointerType(targetTy)) << "from pointer to non pointer is not possible";
		//			targetTy = targetTy.as<core::RefTypePtr>()->getElementType();
		//			return builder.callExpr(mgr.getLangExtension<core::lang::IRppExtensions>().getDynamicCast(), expr, builder.getTypeLiteral((targetTy)));
		//		}

		//		// NORE: All value casts are upgraded to CPP ref, this has no implications for the generated code since lvalues in clang are already refs.
		//		// 		 and makes everithing smoother and easyer
		//		if(exprTy.isa<core::RefTypePtr>()) {
		//			expr = builder.toCppRef(expr);
		//			exprTy = expr.getType();
		//		}

		//		if(core::analysis::isCppRef(exprTy)) {
		//			if(!core::analysis::isCppRef(targetTy)) { targetTy = core::analysis::getCppRef(targetTy); }
		//			retIr = builder.callExpr(mgr.getLangExtension<core::lang::IRppExtensions>().getDynamicCastRefCppToRefCpp(), expr,
		//			                         builder.getTypeLiteral((targetTy)));
		//		} else if(core::analysis::isConstCppRef(exprTy)) {
		//			if(!core::analysis::isCppRef(targetTy)) { targetTy = core::analysis::getConstCppRef(targetTy); }
		//			retIr = builder.callExpr(mgr.getLangExtension<core::lang::IRppExtensions>().getDynamicCastConstCppToConstCpp(), expr,
		//			                         builder.getTypeLiteral((targetTy)));
		//		} else {
		//			std::cerr << " === Dynamic cast  FAILED ===========" << std::endl;
		//			std::cerr << "####### Expr: #######" << std::endl;
		//			std::cerr << (expr);
		//			std::cerr << "\n####### Expr Type: #######" << std::endl;
		//			std::cerr << (exprTy);
		//			std::cerr << "\n####### cast Type: #######" << std::endl;
		//			std::cerr << (targetTy);
		//			std::cerr << "\n####### clang: #######" << std::endl;
		//			castExpr->dump();
		//			abort();
		//		}

		//		return retIr;
		//	}

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//case clang::CK_NullToMemberPointer:
		//	/*case clang::CK_NullToMemberPointer - Null pointer constant to member pointer.
		//	 * int A::*mptr = 0; int (A::*fptr)(int) = nullptr;
		//	 */
		//	{ return builder.callExpr(targetTy, gen.getNullFunc(), builder.getTypeLiteral(targetTy)); }

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//case clang::CK_UserDefinedConversion:
		//	/*case clang::CK_UserDefinedConversion - Conversion using a user defined type conversion function.i
		//	* struct A { operator int(); }; int i = int(A());
		//	* */
		//	{ return converter.convertExpr(castExpr->getSubExpr()); }

		//case clang::CK_AtomicToNonAtomic:
		//case clang::CK_NonAtomicToAtomic:
		//case clang::CK_CopyAndAutoreleaseBlockObject:
		//	std::cout << " \nCAST: " << castExpr->getCastKindName() << " not supported!!" << std::endl;
		//	std::cout << " at location: " << frontend::utils::location(castExpr->getLocStart(), converter.getSourceManager()) << std::endl;
		//	castExpr->dump();
		//	assert_fail();
		default: break; // fall through to not implemented assertion below
		}

		frontend_assert(false) << "Clang cast type not implemented: " << castExpr->getCastKindName();
		return expr;
	}
Beispiel #4
0
void Pack::initialize(const std::string& name,
                      const std::string& source,
                      const pt::ptree& tree) {
    name_ = name;
    source_ = source;
    // Check the shard limitation, shards falling below this value are included.
    if (tree.count("shard") > 0) {
        shard_ = tree.get<size_t>("shard", 0);
    }

    // Check for a platform restriction.
    platform_.clear();
    if (tree.count("platform") > 0) {
        platform_ = tree.get<std::string>("platform", "");
    }

    // Check for a version restriction.
    version_.clear();
    if (tree.count("version") > 0) {
        version_ = tree.get<std::string>("version", "");
    }

    // Apply the shard, platform, and version checking.
    // It is important to set each value such that the packs meta-table can report
    // each of the restrictions.
    if ((shard_ > 0 && shard_ < getMachineShard()) || !checkPlatform() ||
            !checkVersion()) {
        return;
    }

    discovery_queries_.clear();
    if (tree.count("discovery") > 0) {
        for (const auto& item : tree.get_child("discovery")) {
            discovery_queries_.push_back(item.second.get_value<std::string>());
        }
    }

    // Initialize a discovery cache at time 0.
    discovery_cache_ = std::make_pair<size_t, bool>(0, false);
    valid_ = true;

    // If the splay percent is less than 1 reset to a sane estimate.
    if (FLAGS_schedule_splay_percent <= 1) {
        FLAGS_schedule_splay_percent = 10;
    }

    schedule_.clear();
    if (tree.count("queries") == 0) {
        // This pack contained no queries.
        return;
    }

    // Iterate the queries (or schedule) and check platform/version/sanity.
    for (const auto& q : tree.get_child("queries")) {
        if (q.second.count("shard") > 0) {
            auto shard = q.second.get<size_t>("shard", 0);
            if (shard > 0 && shard < getMachineShard()) {
                continue;
            }
        }

        if (q.second.count("platform")) {
            if (!checkPlatform(q.second.get<std::string>("platform", ""))) {
                continue;
            }
        }

        if (q.second.count("version")) {
            if (!checkVersion(q.second.get<std::string>("version", ""))) {
                continue;
            }
        }

        ScheduledQuery query;
        query.query = q.second.get<std::string>("query", "");
        query.interval = q.second.get("interval", FLAGS_schedule_default_interval);
        if (query.interval <= 0 || query.query.empty() || query.interval > 592200) {
            // Invalid pack query.
            VLOG(1) << "Query has invalid interval: " << q.first << ": "
                    << query.interval;
            continue;
        }

        query.splayed_interval = restoreSplayedValue(q.first, query.interval);
        query.options["snapshot"] = q.second.get<bool>("snapshot", false);
        query.options["removed"] = q.second.get<bool>("removed", true);
        schedule_[q.first] = query;
    }
}
Beispiel #5
0
// WARNING:
//      This function contains an unusual amount of raw pointers, and is
//      the most C-like function you can get without being C++.
//      Sorry Joshua.
void Text::render() {
    int width = this->width;
    int height = this->height;
    std::pair<int,int> window_size = window->get_window_size();
    // Automatic sizing if dimension is 0.
    if (width == 0) {
        switch (alignment_h) {
        default:
        case Alignment::LEFT:
            width = window_size.first - this->x;
            break;
        case Alignment::CENTRE:
            width = ((window_size.first / 2) < this->x) ? this->x
                                                        : window_size.first - this->x;
            break;
        case Alignment::RIGHT:
            width = this->x;
            break;
        }
    }
    if (height == 0) {
        switch (alignment_v) {
        default:
        case Alignment::TOP:
            height = this->y;
            break;
        case Alignment::CENTRE:
            height = ((window_size.second / 2) < this->y) ? window_size.second - this->y
                                                          : this->y;
            break;
        case Alignment::BOTTOM:
            height = window_size.second - this->y;
            break;
        }
    }

    // If they are still zero, don't continue.
    if (width == 0 || height == 0) {
        throw Text::RenderException("Invalid dimensions after auto-sizing.");
    }

    int available_width = width;
    // It took a whole day to discover that there was a bug in
    // SDL_ttf. Starting with certain characters on certain
    // fonts seems to break it. :(
    // As a hack, prepend and (for balance) append a space.
    int border;
    TTF_SizeUTF8(font.font, " ", &border, NULL);
    border *= 2;

    // If they are still zero, don't continue.
    if (available_width <= 0) {
        throw Text::RenderException("No available width for rendering text.");
    }

    int line_height = TTF_FontHeight(font.font);
    int line_number = 0;
    int lost_lines = 0;

    int used_width = 0;

    int length = (int)text.length();
    // Entire text.
    const char* ctext = text.c_str();

    // Line of text.
    char* line = new char[length+1];

    // Null-terminator separated lines of text.
    // The worst case is a character per line, so we need to do
    // 2 * length (character, null, character, null, ...).
    char* lines = new char[2*length+1];
    // Pointer to within lines.
    char* lines_scan = lines;

    // Text indexing.
    for (int t = 0; t < length; t++) {
        int line_width = 0;
        // Word indexing.
        int w = 0;
        // Stores (from the beginning) a word in ctext (copy).
        line[0] = '\0';
        // Points to the end of the last successfully fitting word in
        // line.
        int ll = 0;

        // Line indexing.
        for (int l = t; l < length; l++) {
            // Find word end.
            for (w = l; w < length && (ctext[w] != ' ' && ctext[w] != '\n'); w++) {
                // As we are dealing with UTF-8, we must make sure to
                // copy an entire character if it is more than one byte.
                // The bit formats of UTF-8 characters are:
                // 0xxxxxxx
                // 110xxxxx 10xxxxxx
                // 1110xxxx 10xxxxxx 10xxxxxx
                // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
                // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
                // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
                //
                // ... Well, actually it's not THAT simple, because
                // there are these magical characters which modify other
                // characters... But it's not worth the effort.
                if (ctext[l] & 0x80) {
                    // The number of bits in the first byte set to 1
                    // before the first zero indicate the total number
                    // of bytes to use for the UTF-8 character.
                    for (int bits = ctext[w] << 1; w < length && (bits & 0x80); w++, bits <<= 1);
                }
            }
            // Copy word into line.
            for (; l < w; l++) {
                line[l - t] = ctext[l];
            }
            line[l - t] = '\0';
            // Test line length.
            TTF_SizeUTF8(font.font, line, &line_width, NULL);
            // Part of SDL_ttf bug workaround.
            line_width+=border;
            if (line_width <= available_width) {
                if (line_width > used_width) {
                    used_width = line_width;
                }
                // Mark current position as valid.
                ll = l - t;
                // If this is the end of a line, or whole text, stop.
                if (ctext[w] == '\n' || ctext[w] == '\0') {
                    t = l;
                    // Note the string is already null terminated.
                    break;
                }
                else {
                    // Replace null terminator with word separator.
                    line[ll] = ctext[w];
                }
            }
            else {
                if (ll > 0) {
                    // There are some fittable words.
                    // As the line overflows with the current l, set the
                    // text index to the last fit.
                    t += ll;
                }
                else {
                    // There are no separators to break on.
                    // We're going to have to cut a word in half.

                    // Swapped-out character
                    char c;
                    int left = 0;
                    int right = l - t;
                    // Search index within line.
                    int ls;
                    ll = 0;
                    for (ls = (right + left) / 2; ls != ll; ls = (right + left) / 2) {
                        ll = ls;
                        c = line[ls];
                        line[ls] = 0;
                        TTF_SizeUTF8(font.font, line, &line_width, NULL);
                        // Part of SDL_ttf bug workaround.
                        line_width+=border;
                        if (line_width <= available_width) {
                            if (line_width > used_width) {
                                used_width = line_width;
                            }
                            left = ls;
                        }
                        else {
                            right = ls;
                        }
                        line[ls] = c;
                    }
                    // Don't skip the non-separating character.
                    t += ll - 1;
                }
                // Null terminate for rendering.
                line[ll] = '\0';
                break;
            }
        }

        // Copy the line into lines (using lines_scan).
        int i;
        for (i = 0; line[i] != '\0'; ++i) {
            lines_scan[i] = line[i];
        }
        lines_scan[i] = '\0';
        lines_scan = &lines_scan[i+1];

        ++line_number;
        if (line[0] == '\0') {
            // Check that we aren't going to chase our tailes trying to fit an unfittable character.
            if (ctext[w] != '\n') {
                LOG(WARNING) << "Cannot render text: character too large.";
                delete[] line;
                delete[] lines;
                throw Text::RenderException("A character is too large");
            }
            else {
                // It's a blank line.
                continue;
            }
        }
    }
    int line_count = line_number;

    int used_height = line_count * line_height;

    used_width += border;

    image = Image(used_width, (used_height < height) ? used_height : height, true);
    uint8_t clear_colour[4] = {rgba[0], rgba[1], rgba[2], 0x00};
    uint8_t clear_mask[4] = {0xff, 0xff, 0xff, 0xff};
    image.clear(clear_colour, clear_mask);
    // image.clear(0xffffff00, 0xffffffff);

    // Render all lines of text.
    SDL_Color blank;
    blank.r = blank.g = blank.b = blank.a = 0;
    SDL_Color colour;
    colour.r = rgba[0];
    colour.g = rgba[1];
    colour.b = rgba[2];
    colour.a = rgba[3];
    lines_scan = lines;
    for (int line_number = 0; line_number < line_count; ++line_number) {
        // Render line
        VLOG(2) << "Rendering line of text: \"" << lines_scan << "\".";
        if (lines_scan[0] == '\0') {
            // Skip it - it's a new line.
            lines_scan = &lines_scan[1];
            continue;
        }

        SDL_Surface* rendered_line;
        {
            // It took a whole day to discover that there was a bug in
            // SDL_ttf. Starting with certain characters on certain
            // fonts seems to break it. :(
            // As a hack, prepend and (for balance) append a space.
            std::stringstream safe;
            safe << " " << lines_scan << " ";
            std::string safe_str(safe.str());
            if (smooth) {
                rendered_line = TTF_RenderUTF8_Shaded(font.font, safe_str.c_str(), colour, blank);
            }
            else {
                rendered_line = TTF_RenderUTF8_Solid(font.font, safe_str.c_str(), colour);
            }
        }

        if (rendered_line == nullptr) {
            LOG(WARNING) << "Cannot render line of text: \"" << lines_scan << "\".";
            delete[] line;
            delete[] lines;
            throw Text::RenderException("Cannot render line of text");
        }

#ifdef TEXT_SAFE_SURFACE
        // This surface has a known format.
        SDL_Surface* compatible = SDL_CreateRGBSurface(0, // Unsed
                                                       rendered_line->w,
                                                       rendered_line->h,
                                                       32,
                                                       0x00,
                                                       0x00,
                                                       0x00,
                                                       0xff
                                                       );
        SDL_FillRect(compatible, NULL, 0);
        SDL_SetSurfaceBlendMode(rendered_line, SDL_BLENDMODE_NONE);
        SDL_BlitSurface(rendered_line, NULL, compatible, NULL);
        SDL_LockSurface(compatible);
        // pitch is in bytes, not pixels. RGBA = 4 bytes.
        int jump = compatible->pitch / 4;
#else
        SDL_LockSurface(rendered_line);
        int jump = rendered_line->pitch;
#endif
        // "What is this pointless copy?" you might ask.
        //   Well, it is a suggestion to the c++ compiler that smooth
        //   will not change throughout this function, so it can
        //   optimise the if containing _smooth out of the for loop
        //   entirely, rather than checking it every single iteration.
        int _smooth = smooth;
        int x_offset;
        int y_offset;
        switch (alignment_h) {
        default:
        case Alignment::LEFT:
            x_offset = 0;
            break;
        case Alignment::CENTRE:
            x_offset = (used_width - rendered_line->w) / 2;
            break;
        case Alignment::RIGHT:
            x_offset = used_width - rendered_line->w;
            break;
        }
        switch (alignment_v) {
        default:
        case Alignment::TOP:
            y_offset = line_number * line_height;
            break;
        case Alignment::CENTRE:
            y_offset = line_number * line_height - (used_height - image.height) / 2;
            break;
        case Alignment::BOTTOM:
            y_offset = line_number * line_height - (used_height - image.height);
            break;
        }
        // x surface
        int xs;
        // y surface
        int ys;
        for (ys = 0; ys < rendered_line->h; ++ys) {
            int yi = ys + y_offset;
            if (yi >= image.height) {
                lost_lines++;
                break;
            }
            else if (yi < 0) {
                continue;
            }
            int begin_xs((x_offset >= 0) ? 0 : -x_offset);
            int end_xs((rendered_line->w < image.width - x_offset) ? rendered_line->w : image.width - x_offset);
            for (xs = begin_xs; xs < end_xs; ++xs) {
#ifdef TEXT_SAFE_SURFACE
                image.flipped_pixels[yi][xs + x_offset].a = (((Uint32*)compatible->pixels)[(ys*jump + xs)]);
#else
                if (_smooth) {
                    image.flipped_pixels[yi][xs + x_offset].a = (((Uint8*)rendered_line->pixels)[(ys*jump + xs)]);
                }
                else {
                    image.flipped_pixels[yi][xs + x_offset].a = (((Uint8*)rendered_line->pixels)[(ys*jump + xs)]) ? 255 : 0;
                }
#endif
            }
        }
#ifdef TEXT_SAFE_SURFACE
        SDL_UnlockSurface(compatible);
        SDL_FreeSurface(compatible);
#else
        SDL_UnlockSurface(rendered_line);
#endif
        SDL_FreeSurface(rendered_line);
        if (ys < line_height) {
            LOG(WARNING) << "Text overflow.";
            break;
        }

        // Set lines_scan to start next line
        while (lines_scan[0] != '\0') {
            lines_scan = &lines_scan[1];
        }
        lines_scan = &lines_scan[1];
    }

    this->used_width  = used_width;
    this->used_height = used_height;
    delete[] line;
    delete[] lines;
    generate_texture();
    dirty_texture = false;
    dirty_vbo = true;
}
void FrameTransport::onComplete() noexcept {
  VLOG(6) << "onComplete";
  terminateFrameProcessor(folly::exception_wrapper());
}
FrameTransport::~FrameTransport() {
  VLOG(6) << "~FrameTransport";
}
Beispiel #8
0
int
main(int argc, char *argv[])
{
    extern struct vlog_module VLM_reconnect;
    struct ovsdb_idl *idl;
    struct ctl_command *commands;
    struct shash local_options;
    unsigned int seqno;
    size_t n_commands;
    char *args;

    set_program_name(argv[0]);
    fatal_ignore_sigpipe();
    vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
    vlog_set_levels(&VLM_reconnect, VLF_ANY_DESTINATION, VLL_WARN);
    sbrec_init();

    sbctl_cmd_init();

    /* Log our arguments.  This is often valuable for debugging systems. */
    args = process_escape_args(argv);
    VLOG(ctl_might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);

    /* Parse command line. */
    shash_init(&local_options);
    parse_options(argc, argv, &local_options);
    commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
                                  &n_commands);

    if (timeout) {
        time_alarm(timeout);
    }

    /* Initialize IDL. */
    idl = the_idl = ovsdb_idl_create(db, &sbrec_idl_class, false, false);
    run_prerequisites(commands, n_commands, idl);

    /* Execute the commands.
     *
     * 'seqno' is the database sequence number for which we last tried to
     * execute our transaction.  There's no point in trying to commit more than
     * once for any given sequence number, because if the transaction fails
     * it's because the database changed and we need to obtain an up-to-date
     * view of the database before we try the transaction again. */
    seqno = ovsdb_idl_get_seqno(idl);
    for (;;) {
        ovsdb_idl_run(idl);
        if (!ovsdb_idl_is_alive(idl)) {
            int retval = ovsdb_idl_get_last_error(idl);
            ctl_fatal("%s: database connection failed (%s)",
                        db, ovs_retval_to_string(retval));
        }

        if (seqno != ovsdb_idl_get_seqno(idl)) {
            seqno = ovsdb_idl_get_seqno(idl);
            do_sbctl(args, commands, n_commands, idl);
        }

        if (seqno == ovsdb_idl_get_seqno(idl)) {
            ovsdb_idl_wait(idl);
            poll_block();
        }
    }
}
Beispiel #9
0
Spectrum PathIntegrator::Li(const RayDifferential &r, const Scene &scene,
                            Sampler &sampler, MemoryArena &arena,
                            int depth) const {
    ProfilePhase p(Prof::SamplerIntegratorLi);
    Spectrum L(0.f), beta(1.f);
    RayDifferential ray(r);
    bool specularBounce = false;
    int bounces;
    for (bounces = 0;; ++bounces) {
        // Find next path vertex and accumulate contribution
        VLOG(2) << "Path tracer bounce " << bounces << ", current L = " << L <<
            ", beta = " << beta;

        // Intersect _ray_ with scene and store intersection in _isect_
        SurfaceInteraction isect;
        bool foundIntersection = scene.Intersect(ray, &isect);

        // Possibly add emitted light at intersection
        if (bounces == 0 || specularBounce) {
            // Add emitted light at path vertex or from the environment
            if (foundIntersection) {
                L += beta * isect.Le(-ray.d);
                VLOG(2) << "Added Le -> L = " << L;
            } else {
                for (const auto &light : scene.infiniteLights)
                    L += beta * light->Le(ray);
                VLOG(2) << "Added infinite area lights -> L = " << L;
            }
        }

        // Terminate path if ray escaped or _maxDepth_ was reached
        if (!foundIntersection || bounces >= maxDepth) break;

        // Compute scattering functions and skip over medium boundaries
        isect.ComputeScatteringFunctions(ray, arena, true);
        if (!isect.bsdf) {
            VLOG(2) << "Skipping intersection due to null bsdf";
            ray = isect.SpawnRay(ray.d);
            bounces--;
            continue;
        }

        const Distribution1D *distrib = lightDistribution->Lookup(isect.p);

        // Sample illumination from lights to find path contribution.
        // (But skip this for perfectly specular BSDFs.)
        if (isect.bsdf->NumComponents(BxDFType(BSDF_ALL & ~BSDF_SPECULAR)) >
            0) {
            ++totalPaths;
            Spectrum Ld =
                beta * UniformSampleOneLight(isect, scene, arena, sampler, false,
                                             distrib);
            VLOG(2) << "Sampled direct lighting Ld = " << Ld;
            if (Ld.IsBlack()) ++zeroRadiancePaths;
            CHECK_GE(Ld.y(), 0.f);
            L += Ld;
        }

        // Sample BSDF to get new path direction
        Vector3f wo = -ray.d, wi;
        Float pdf;
        BxDFType flags;
        Spectrum f = isect.bsdf->Sample_f(wo, &wi, sampler.Get2D(), &pdf,
                                          BSDF_ALL, &flags);
        VLOG(2) << "Sampled BSDF, f = " << f << ", pdf = " << pdf;
        if (f.IsBlack() || pdf == 0.f) break;
        beta *= f * AbsDot(wi, isect.shading.n) / pdf;
        VLOG(2) << "Updated beta = " << beta;
        CHECK_GE(beta.y(), 0.f);
        DCHECK(!std::isinf(beta.y()));
        specularBounce = (flags & BSDF_SPECULAR) != 0;
        ray = isect.SpawnRay(wi);

        // Account for subsurface scattering, if applicable
        if (isect.bssrdf && (flags & BSDF_TRANSMISSION)) {
            // Importance sample the BSSRDF
            SurfaceInteraction pi;
            Spectrum S = isect.bssrdf->Sample_S(
                scene, sampler.Get1D(), sampler.Get2D(), arena, &pi, &pdf);
            DCHECK(!std::isinf(beta.y()));
            if (S.IsBlack() || pdf == 0) break;
            beta *= S / pdf;

            // Account for the direct subsurface scattering component
            L += beta * UniformSampleOneLight(pi, scene, arena, sampler, false,
                                              lightDistribution->Lookup(pi.p));

            // Account for the indirect subsurface scattering component
            Spectrum f = pi.bsdf->Sample_f(pi.wo, &wi, sampler.Get2D(), &pdf,
                                           BSDF_ALL, &flags);
            if (f.IsBlack() || pdf == 0) break;
            beta *= f * AbsDot(wi, pi.shading.n) / pdf;
            DCHECK(!std::isinf(beta.y()));
            specularBounce = (flags & BSDF_SPECULAR) != 0;
            ray = pi.SpawnRay(wi);
        }

        // Possibly terminate the path with Russian roulette
        if (beta.y() < rrThreshold && bounces > 3) {
            Float q = std::max((Float).05, 1 - beta.MaxComponentValue());
            VLOG(2) << "RR termination probability q = " << q;
            if (sampler.Get1D() < q) break;
            beta /= 1 - q;
            VLOG(2) << "After RR survival, beta = " << beta;
            DCHECK(!std::isinf(beta.y()));
        }
    }
    ReportValue(pathLength, bounces);
    return L;
}
Beispiel #10
0
static casserr_t mo5_k5_load( cassette_image *cass )
{
	size_t size = cassette_image_size( cass ), pos = 0;
	int i, sz, sz2, hbit = 0;
	UINT8 in, in2, in3, typ, block[264], sum;
	int invalid = 0, hbitsize = 0, dcmoto = 0;

	LOG (( "mo5_k5_load: start conversion, size=%li\n", (long)size ));
	PRINT (( "mo5_k5_load: open cassette, length: %li bytes\n", (long) size ));

	/* store a half-bit */
#define K5_PUT_HBIT							\
	do								\
	{								\
		cassette_put_sample ( cass, 0, hbitsize * MO5_HBIT_LENGTH, MO5_HBIT_LENGTH, (hbit ? 1 : -1) << 30 ); \
		hbitsize++;						\
	} while ( 0 )

	/* store one bit */
#define K5_PUT_BIT( BIT )			\
	do					\
	{					\
		if ( BIT )			\
		{				\
			K5_PUT_HBIT;		\
			hbit = !hbit;		\
			K5_PUT_HBIT;		\
		}				\
		else				\
		{				\
			K5_PUT_HBIT;		\
			K5_PUT_HBIT;		\
		}				\
		hbit = !hbit;			\
	} while (0)

	/* store one byte, no start / stop bit, converse order from TO7 */
#define K5_PUT_BYTE( BYTE ) \
	do							\
	{							\
		UINT8 b = BYTE;					\
		int x;						\
		for ( x = 0; x < 8; x++ )			\
			K5_PUT_BIT( (b >> (7 - x)) & 1 );	\
	} while (0)

	/* store filler */
#define K5_FILL_0( SIZE ) \
	do								\
	{								\
		if ( (SIZE) > 0 )					\
		{							\
			int j;						\
			LOG (( "mo5_k5_load: 0-filler size=%i hbitstart=%i\n", (SIZE), hbitsize )); \
			for ( j = 0; j < (SIZE); j++ )			\
				K5_PUT_BIT( 0 );			\
		}							\
	} while (0)

#define K5_FILL_01( SIZE )						\
	do								\
	{								\
		if ( (SIZE) > 0 )					\
		{							\
			int j;						\
			LOG (( "mo5_k5_load: 0x01 filler size=%i bitstart=%i\n", (SIZE), hbitsize )); \
			for ( j = 0; j < (SIZE); j++ )			\
				K5_PUT_BYTE( 0x01 );			\
		}							\
	} while (0)

	/* check format */
	cassette_image_read( cass, block, 0, 64 );
	for ( i = 3; ; i++ )
	{
		if ( ( i >= size ) || ( i >= 64 ) )
		{
			/* ? */
			PRINT (( "to5_k5_load: WARNING: this does not look like a MO or TO cassette.\n" ));
			break;
		}
		else if ( ( block[i-3] == 0x01 ) && ( block[i-2] == 0x3c ) && ( block[i-1] == 0x5a ) && ! block[i] )
		{
			/* MO */
			break;
		}
		else if ( ( block[i-3] == 0xff ) && ( block[i-2] == 0x01 ) && ( block[i-1] == 0x3c ) && ! block[i] )
		{
			/* TO */
			PRINT (( "to5_k5_load: WARNING: this looks like a TO cassette, not a MO one.\n" ));
			break;
		}
	}

	cassette_image_read( cass, block, pos, 6 );
	if ( ! memcmp( block, "DCMOTO", 6 ) || ! memcmp( block, "DCMO5", 5 ) || ! memcmp( block, "DCMO6", 5 ) )
		dcmoto = 1;

	/* loop over regular blocks */
	while ( pos < size )
	{

	rebounce:
		/* skip DCMOTO header*/
		if ( dcmoto )
		{
			cassette_image_read( cass, block, pos, 6 );
			if ( ! memcmp( block, "DCMOTO", 6 ) )
			{
				LOG (( "mo5_k5_load: DCMOTO signature found at off=$%x\n", (int)pos ));
				pos += 6;
			}
			else if ( ! memcmp( block, "DCMO", 4 ) )
			{
				LOG (( "mo5_k5_load: DCMO* signature found at off=$%x\n", (int)pos ));
				pos += 5;
			}
		}

		/* skip 0x01 filler */
		for ( sz = 0; pos < size; pos++, sz++ )
		{
			cassette_image_read( cass, &in, pos, 1 );
			if ( in != 0x01 )
				break;
		}

		/* get block header */
		if ( pos + 4 > size )
		{
			pos -= sz;
			break;
		}
		cassette_image_read( cass, block, pos, 4 );
		typ = block[2];
		sz2 = (UINT8) (block[3]-1);
		if ( block[0] != 0x3c || block[1] != 0x5a || ( typ != 0x00 && typ != 0x01 && typ !=  0xff ) || pos+sz2 > size )
		{
			pos -= sz;
			break;
		}
		pos += 4;

		/* get block */
		cassette_image_read( cass, block+4, pos, sz2 );
		pos += sz2;

		/* 0-fillers and 0x01-fillers */
		if ( typ == 0 || typ == 0xff )
			K5_FILL_0( 1200 );
		else
			K5_FILL_0( 300 ); /* for MO6 */
		K5_FILL_01( sz < 10 ? 10 : sz );

		/* put block */
		LOG (( "mo5_k5_load: block off=$%x type=$%02X size=%i hbitstart=%i\n", (int) pos-sz2-4, typ, sz2, hbitsize ));
		VLOG (( "mo5_k5_load: data:" ));
		for ( i = 0; i < sz2 + 4; i ++)
		{
			VLOG (( " $%02X", block[i] ));
			K5_PUT_BYTE( block[i] );
		}
		VLOG (( "\n" ));

		/* checksum */
		for ( i = 0, sum = 0; i < sz2; i++ )
			sum += block[i+4];
		if ( sum )
			LOG(( "mo5_k5_load: invalid checksum $%02X (should be 0)\n", sum ));

		/* if it is a directory enty, says so to the user */
		if ( typ == 0 )
		{
			char name[] = "01234567.ABC";
			UINT8 t = block[15];
			UINT8 u = block[16];
			int p = (hbitsize - sz2 - 4 - sz) * MO5_HBIT_LENGTH;
			memcpy( name, block+4, 8 );
			memcpy( name+9, block+12, 3 );
			for ( i = 0; name[i]; i++)
			{
				if ( name[i] < ' ' || name[i] >= 127 )
					name[i] = '?';
			}
			PRINT (( "mo5_k5_load: file \"%s\" type=%s,%s at %imn %is\n",
				 name,
				 (t==0) ? "bas" : (t==1) ? "dat" : (t==2) ? "bin" : "???",
				 (u == 0) ? "a" : (u == 0xff) ? "b" : "?",
				 p / 60, p % 60 ));
		}

		/* 0-fillers */
		if ( typ == 0xff || typ == 0x00 )
			K5_FILL_0( 1800 );
	}

	/* dump trailing bytes, but also look for beginings of blocks */
	if ( pos < size )
	{
		invalid++;
		K5_FILL_0( 1200 );
		LOG (( "mo5_k5_load: trailing trash off=$%x size=%i hbitstart=%i\n", (int) pos, (int) (size-pos), hbitsize ));
		for ( ; pos < size; pos++ )
		{
			cassette_image_read( cass, &in, pos, 1 );
			if ( dcmoto && in=='D' )
			{
				/* skip DCMOTO header*/
				cassette_image_read( cass, block, pos, 6 );
				if ( ! memcmp( block, "DCMOTO", 6 ) )
				{
					LOG (( "mo5_k5_load: DCMOTO signature found at off=$%x\n", (int)pos ));
					pos += 6;
					cassette_image_read( cass, &in, pos, 1 );
				}
				else if ( ! memcmp( block, "DCMO", 4 ) )
				{
					LOG (( "mo5_k5_load: DCMO* signature found at off=$%x\n", (int)pos ));
					pos += 5;
					cassette_image_read( cass, &in, pos, 1 );
				}
			}
			for ( sz = 0; pos < size && in == 0x01; sz++ )
			{
				pos++;
				cassette_image_read( cass, &in, pos, 1 );
			}
			if ( sz > 6 )
			{
				cassette_image_read( cass, &in2, pos+1, 1 );
				cassette_image_read( cass, &in3, pos+2, 1 );
				if ( invalid < 10 &&  in == 0x3c && in2 == 0x5a && (in3 == 0x00 || in3 == 0x01 || in3 == 0xff ) )
				{
					/* regular block found */
					LOG (( "mo5_k5_load: hidden regular block found\n" ));
					pos -= sz;
					goto rebounce;
				}
				if ( invalid < 10 && sz > 6 && ( (in == 0x3c && in2 == 0x5a) || (in == 0xc3 && in2 == 0x5a) || (in == 0xc3 && in2 == 0x3c) || (in == 0x87 && in2 == 0x4a)  ) )
				{
					/* special block found */
					K5_FILL_0( 1200 );
					LOG (( "mo5_k5_load: special block $%02X $%02X found off=$%x hbitstart=%i\n", in, in2, (int) pos-sz, hbitsize ));
				}
			}

			VLOG (( "mo5_k5_load: special data:" ));
			for ( i = 0; i < sz; i++ )
			{
				K5_PUT_BYTE( 0x01 );
				VLOG (( " $01" ));
			}
			K5_PUT_BYTE( in );
			VLOG (( " $%02X\n", in ));
		}
	}

	if ( invalid )
		PRINT (( "mo5_k5_load: WARNING: the k5 has an unknown structure and may not work\n" ));

	sz = hbitsize * MO5_HBIT_LENGTH;
	PRINT (( "mo5_k5_load: cassette length: %imn %is (%i half-bits)\n", sz / 60, sz % 60, hbitsize ));

	return CASSETTE_ERROR_SUCCESS;
}
Beispiel #11
0
void SolverLinearPetsc<T>::init ()
{
    // Initialize the data structures if not done so already.
    if ( !this->initialized() )
    {
        this->setInitialized(  true );

        int ierr=0;

        // 2.1.x & earlier style
#if (PETSC_VERSION_MAJOR == 2) && (PETSC_VERSION_MINOR <= 1)

        // Create the linear solver context
        ierr = SLESCreate ( this->worldComm().globalComm(), &M_sles );
        CHKERRABORT( this->worldComm().globalComm(),ierr );

        // Create the Krylov subspace & preconditioner contexts
        ierr = SLESGetKSP       ( M_sles, &M_ksp );
        CHKERRABORT( this->worldComm().globalComm(),ierr );
        ierr = SLESGetPC        ( M_sles, &M_pc );
        CHKERRABORT( this->worldComm().globalComm(),ierr );

        // Have the Krylov subspace method use our good initial guess rather than 0
        ierr = KSPSetInitialGuessNonzero ( M_ksp, PETSC_TRUE );
        CHKERRABORT( this->worldComm().globalComm(),ierr );

        // Set user-specified  solver and preconditioner types
        this->setPetscSolverType();
        this->setPetscPreconditionerType();
        this->setPetscConstantNullSpace();

        // Set the options from user-input
        // Set runtime options, e.g.,
        //      -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
        //  These options will override those specified above as long as
        //  SLESSetFromOptions() is called _after_ any other customization
        //  routines.

        ierr = SLESSetFromOptions ( M_sles );
        CHKERRABORT( this->worldComm().globalComm(),ierr );

        // 2.2.0 & newer style
#else

        // Create the linear solver context
        ierr = KSPCreate ( this->worldComm().globalComm(), &M_ksp );
        CHKERRABORT( this->worldComm().globalComm(),ierr );

        // Create the preconditioner context
        ierr = KSPGetPC        ( M_ksp, &M_pc );
        CHKERRABORT( this->worldComm().globalComm(),ierr );

        // Have the Krylov subspace method use our good initial guess rather than 0
        ierr = KSPSetInitialGuessNonzero ( M_ksp, PETSC_TRUE );
        CHKERRABORT( this->worldComm().globalComm(),ierr );

        // Set user-specified  solver and preconditioner types
        this->setPetscSolverType();
        this->setPetscConstantNullSpace();


        // Set the options from user-input
        // Set runtime options, e.g.,
        //      -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
        //  These options will override those specified above as long as
        //  KSPSetFromOptions() is called _after_ any other customization
        //  routines.
        //ierr = PCSetFromOptions ( M_pc );
        //CHKERRABORT( this->worldComm().globalComm(),ierr );
        ierr = KSPSetFromOptions ( M_ksp );
        CHKERRABORT( this->worldComm().globalComm(),ierr );


#endif

        // Have the Krylov subspace method use our good initial guess
        // rather than 0, unless the user requested a KSPType of
        // preonly, which complains if asked to use initial guesses.
#if PETSC_VERSION_LESS_THAN(3,0,0)
        KSPType ksp_type;
#else
#if PETSC_VERSION_LESS_THAN(3,4,0)
        const KSPType ksp_type;
#else
        KSPType ksp_type;
#endif
#endif

        ierr = KSPGetType ( M_ksp, &ksp_type );
        CHKERRABORT( this->worldComm().globalComm(),ierr );

        if ( std::string((char*)ksp_type) == std::string( ( char* )KSPPREONLY ) )
        {
            ierr = KSPSetInitialGuessNonzero ( M_ksp, PETSC_FALSE );
            CHKERRABORT( this->worldComm().globalComm(),ierr );
        }

        if ( std::string((char*)ksp_type) == std::string( ( char* )KSPGMRES ) )
        {
            int nRestartGMRES = option(_name="gmres-restart", _prefix=this->prefix() ).template as<int>();
            ierr = KSPGMRESSetRestart( M_ksp, nRestartGMRES );
            CHKERRABORT( this->worldComm().globalComm(),ierr );
        }
        // Notify PETSc of location to store residual history.
        // This needs to be called before any solves, since
        // it sets the residual history length to zero.  The default
        // behavior is for PETSc to allocate (internally) an array
        // of size 1000 to hold the residual norm history.
        ierr = KSPSetResidualHistory( M_ksp,
                                      PETSC_NULL,   // pointer to the array which holds the history
                                      PETSC_DECIDE, // size of the array holding the history
                                      PETSC_TRUE ); // Whether or not to reset the history for each solve.
        CHKERRABORT( this->worldComm().globalComm(),ierr );

        //If there is a preconditioner object we need to set the internal setup and apply routines
        if ( this->M_preconditioner )
        {
            VLOG(2) << "preconditioner: "  << this->M_preconditioner << "\n";

            PCSetType(M_pc, PCSHELL);
            PCShellSetName( M_pc, this->M_preconditioner->name().c_str() );
            PCShellSetContext( M_pc,( void* )this->M_preconditioner.get() );
            PCShellSetSetUp( M_pc,__feel_petsc_preconditioner_setup );
            PCShellSetApply( M_pc,__feel_petsc_preconditioner_apply );
            PCShellSetView( M_pc,__feel_petsc_preconditioner_view );
#if PETSC_VERSION_LESS_THAN(3,4,0)
            const PCType pc_type;
#else
            PCType pc_type;
#endif
            ierr = PCGetType ( M_pc, &pc_type );
            CHKERRABORT( this->worldComm().globalComm(),ierr );

            VLOG(2) << "preconditioner set as "  << pc_type << "\n";
        }
        else
        {
            this->setPetscPreconditionerType();
            // sets the software that is used to perform the factorization
            PetscPCFactorSetMatSolverPackage( M_pc,this->matSolverPackageType() );
        }

        if ( Environment::vm(_name="ksp-monitor",_prefix=this->prefix()).template as<bool>() )
        {
            KSPMonitorSet( M_ksp,KSPMonitorDefault,PETSC_NULL,PETSC_NULL );
        }

    }
}
Beispiel #12
0
static casserr_t to7_k7_load( cassette_image *cass )
{
#if ! K7_SPEED_HACK
	static const INT8 square_wave[] = { -128, 127 };
	double time = 0.;
#endif
	size_t size = cassette_image_size( cass ), pos = 0;
	int i, sz, sz2, bitmax = 1024, invalid = 0;
	UINT8 in, typ, block[264];

	LOG (( "to7_k7_load: start conversion, size=%li\n", (long)size ));
	PRINT (( "to7_k7_load: open cassette, length: %li bytes\n", (long) size ));

	if ( to7_k7_bits )
	{
		free( to7_k7_bits );
		to7_k7_bits = NULL;
	}

	to7_k7_bitsize = 0;
	to7_k7_bits = (UINT8*)malloc(bitmax );

/* store one period */
#if K7_SPEED_HACK
#define K7_PUT( PERIOD )
#else
#define K7_PUT( PERIOD ) \
	do								\
	{								\
		casserr_t err;						\
		err = cassette_put_samples( cass, 0, time, (PERIOD), 2, 1, \
					    square_wave, CASSETTE_WAVEFORM_8BIT ); \
		if ( err )						\
			return err;					\
		time += (PERIOD);					\
	} while (0)
#endif

/* store one bit */
#define K7_PUT_BIT( BIT ) \
	do								\
	{								\
		int b;							\
		if ( BIT )						\
		{							\
			for ( b = 0; b < 7; b++ )			\
				K7_PUT( TO7_PERIOD_CASS_1 );		\
		}							\
		else							\
		{							\
			for ( b = 0; b < 5; b++ )			\
				K7_PUT( TO7_PERIOD_CASS_0 );		\
		}							\
		if ( to7_k7_bitsize + 1 >= bitmax )			\
		{							\
			UINT8* a = (UINT8*)malloc(bitmax * 2);		\
			memcpy ( a, to7_k7_bits, bitmax );		\
			bitmax *= 2;					\
			to7_k7_bits = a;				\
		}							\
		to7_k7_bits[ to7_k7_bitsize++ ] = (BIT);		\
	} while (0)

/* store one byte, with start / stop bits */
#define K7_PUT_BYTE( BYTE ) \
	do							\
	{							\
		UINT8 x;					\
		K7_PUT_BIT( 0 );				\
		for ( x = 0; x < 8; x++ )			\
			K7_PUT_BIT( ( (BYTE) >> x ) & 1 );	\
		K7_PUT_BIT( 1 );				\
		K7_PUT_BIT( 1 );				\
	} while (0)

#define K7_FILL_1( SIZE ) \
	do								\
	{								\
		if ( (SIZE) > 0 ) {					\
			int ii;						\
			LOG (( "to7_k7_load: 1-filler size=%i bitstart=%i\n", \
			       (SIZE), to7_k7_bitsize ));		\
			for ( ii = 0; ii < (SIZE); ii++ ) K7_PUT_BIT( 1 ); \
		}							\
	} while (0)

#define K7_FILL_ff( SIZE ) \
	do								\
	{								\
		if ( (SIZE) > 0 )					\
		{							\
			int ii;						\
			LOG (( "to7_k7_load: 0xff filler size=%i bitstart=%i\n",  (SIZE), to7_k7_bitsize )); \
			for ( ii = 0; ii < (SIZE); ii++ )		\
				K7_PUT_BYTE( 0xff );			\
		}							\
	} while (0)

	/* check format */
	cassette_image_read( cass, block, 0, 64 );
	for ( i = 3; ; i++ )
	{
		if ( ( i >= size ) || ( i >= 64 ) )
		{
			/* ? */
			PRINT (( "to7_k7_load: WARNING: this does not look like a MO or TO cassette.\n" ));
			break;
		}
		else if ( ( block[i-3] == 0x01 ) && ( block[i-2] == 0x3c ) && ( block[i-1] == 0x5a ) && ! block[i] )
		{
			/* MO */
			PRINT (( "to7_k7_load: WARNING: this looks like a MO cassette, not a TO one.\n" ));
			break;
		}
		else if ( ( block[i-3] == 0xff ) && ( block[i-2] == 0x01 ) && ( block[i-1] == 0x3c ) && ! block[i] )
		{
			/* TO */
			break;
		}
	}

	/* skip to first 0xff filler */
	for ( sz = 0; pos < size; pos++, sz++ )
	{
		cassette_image_read( cass, &in, pos, 1 );
		if ( in == 0xff )
			break;
	}
	if ( sz > 0 )
		LOG (( "to7_k7_load: skip %i trash bytes\n", sz ));

	/* loop over regular blocks */
	while ( pos < size )
	{

	rebounce:
		/* skip 0xff filler */
		for ( sz = 0; pos < size; pos++, sz++ )
		{
			cassette_image_read( cass, &in, pos, 1 );
			/* actually, we are bit laxist and treat as 0xff bytes with at least
               5 bits out of 8 set to 1
            */
			for ( i = 0; in; in >>= 1 )
				i += (in & 1);
			if ( i < 5 )
				break;
		}

		/* get block header */
		if ( pos + 4 > size )
		{
			pos -= sz;
			break;
		}
		cassette_image_read( cass, block, pos, 4 );
		typ = block[2];
		sz2 = block[3]+1;
		if ( block[0] != 0x01 || block[1] != 0x3c || ( typ != 0x00 && typ != 0x01 && typ !=  0xff ) )
		{
			pos -= sz;
			break;
		}
		pos += 4;

		/* get block */
		cassette_image_read( cass, block+4, pos, sz2 );
		pos += sz2;

		/* 1-filler and 0xff-filler */
		if ( typ == 0 || typ == 0xff )
			K7_FILL_1( 1000 );
		K7_FILL_ff( sz );

		/* put block */
		LOG (( "to7_k7_load: block off=$%x type=$%02X size=%i bitstart=%i\n", (int) pos-sz2-4, typ, sz2, to7_k7_bitsize ));
		VLOG (( "to7_k7_load: data:" ));
		for ( i = 0; i < sz2 + 4; i ++)
		{
			VLOG (( " $%02X", block[i] ));
			K7_PUT_BYTE( block[i] );
		}
		VLOG (( "\n" ));

		/* if it is a directory enty, says so to the user */
		if ( typ == 0 )
		{
			char name[] = "01234567.ABC";
			UINT8 t = block[15];
			UINT8 u = block[16];
			int p = (to7_k7_bitsize - sz2 - 4 - sz) * TO7_BIT_LENGTH;
			memcpy( name, block+4, 8 );
			memcpy( name+9, block+12, 3 );
			for ( i = 0; name[i]; i++)
			{
				if ( name[i] < ' ' || name[i] >= 127 )
					name[i] = '?';
			}
			PRINT (( "to7_k7_load: file \"%s\" type=%s,%s at %imn %is\n",
				 name,
				 (t==0) ? "bas" : (t==1) ? "dat" : (t==2) ? "bin" : "???",
				 (u == 0) ? "a" : (u == 0xff) ? "b" : "?",
				 p / 60, p % 60 ));
		}

		/* extra 1-fillers */
		if ( typ == 0 || typ == 0xff )
			K7_FILL_1( 1000 );
	}

	/* trailing data with invalid block structure
       => dump it in a raw form, but stay alert for hidden block starts
    */
	if ( pos < size )
	{
		invalid++;
		LOG (( "to7_k7_load: trailing raw bytes off=$%x bitstart=%i\n", (int) pos, to7_k7_bitsize ));

		/* put block */
		for (; pos < size; pos++ )
		{
			cassette_image_read( cass, &in, pos, 1 );
			for ( sz = 0; pos < size && in == 0xff; sz++ )
			{
				pos++;
				cassette_image_read( cass, &in, pos, 1 );
			}
			if ( invalid < 10 && sz > 4 && in == 0x01 && pos + 4 <= size )
			{
				UINT8 in1,in2;
				cassette_image_read( cass, &in1,   pos+1, 1 );
				cassette_image_read( cass, &in2, pos+2, 1 );
				if ( (in1 == 0x3c) && ((in2 == 0x00) || (in2 == 0x01) ) )
				{
					/* seems we have a regular block hidden in raw data => rebounce */
					LOG (( "to7_k7_load: hidden regular block found\n" ));
					pos -= sz;
					goto rebounce;
				}
				if ( ( ( in1 == 0x3d ) && ( in2 == 0 ) ) || ( ( in1 == 0x57 ) && ( in2 == 0x49 ) ) )
				{
					/* special block (Infogrames) => just prepend filler */
					K7_FILL_1 ( 500 );
					LOG (( "to7_k7_load: special $%02X $%02X $%02X block found off=$%x bitstart=%i\n", in, in1, in2, (int) pos, to7_k7_bitsize ));
				}
			}
			for ( i = 0; i < sz; i++ )
				K7_PUT_BYTE( 0xff );
			K7_PUT_BYTE( in );
		}
	}

	if ( invalid )
		PRINT (( "to7_k7_load: WARNING: the k7 has an unknown structure and may not work\n" ));

	sz = to7_k7_bitsize * TO7_BIT_LENGTH;
	PRINT (( "to7_k7_load: cassette length: %imn %is (%i samples)\n", sz / 60, sz % 60, to7_k7_bitsize ));

	return CASSETTE_ERROR_SUCCESS;
}
Beispiel #13
0
 /// Dereference cache
 operator T*() { 
   block_until_acquired();
   DVLOG(5) << "WO dereference of " << address_ << " * " << count_;
   VLOG(6) << "pointer_ = " << pointer_;
   return pointer_;
 } 
Beispiel #14
0
void initialize(
    const string& _argv0,
    const Flags& flags,
    bool installFailureSignalHandler)
{
  static Once* initialized = new Once();

  if (initialized->once()) {
    return;
  }

  argv0 = _argv0;

  if (flags.logging_level != "INFO" &&
      flags.logging_level != "WARNING" &&
      flags.logging_level != "ERROR") {
    EXIT(1) << "'" << flags.logging_level << "' is not a valid logging level."
               " Possible values for 'logging_level' flag are: "
               " 'INFO', 'WARNING', 'ERROR'.";
  }

  FLAGS_minloglevel = getLogSeverity(flags.logging_level);

  if (flags.log_dir.isSome()) {
    Try<Nothing> mkdir = os::mkdir(flags.log_dir.get());
    if (mkdir.isError()) {
      EXIT(1) << "Could not initialize logging: Failed to create directory "
              << flags.log_dir.get() << ": " << mkdir.error();
    }
    FLAGS_log_dir = flags.log_dir.get();
    // Do not log to stderr instead of log files.
    FLAGS_logtostderr = false;
  } else {
    // Log to stderr instead of log files.
    FLAGS_logtostderr = true;
  }

  // Log everything to stderr IN ADDITION to log files unless
  // otherwise specified.
  if (flags.quiet) {
    FLAGS_stderrthreshold = 3; // FATAL.

    // FLAGS_stderrthreshold is ignored when logging to stderr instead
    // of log files. Setting the minimum log level gets around this issue.
    if (FLAGS_logtostderr) {
      FLAGS_minloglevel = 3; // FATAL.
    }
  } else {
    FLAGS_stderrthreshold = FLAGS_minloglevel;
  }

  FLAGS_logbufsecs = flags.logbufsecs;

  google::InitGoogleLogging(argv0.c_str());
  if (flags.log_dir.isSome()) {
    // Log this message in order to create the log file; this is because GLOG
    // creates the log file once the first log message occurs; also recreate
    // the file if it has been created on a previous run.
    LOG_AT_LEVEL(FLAGS_minloglevel)
      << google::GetLogSeverityName(FLAGS_minloglevel)
      << " level logging started!";
  }

  VLOG(1) << "Logging to " <<
    (flags.log_dir.isSome() ? flags.log_dir.get() : "STDERR");

  if (installFailureSignalHandler) {
    // Handles SIGSEGV, SIGILL, SIGFPE, SIGABRT, SIGBUS, SIGTERM
    // by default.
    google::InstallFailureSignalHandler();

    // Set up our custom signal handlers.
    struct sigaction action;
    action.sa_sigaction = handler;

    // Do not block additional signals while in the handler.
    sigemptyset(&action.sa_mask);

    // The SA_SIGINFO flag tells sigaction() to use
    // the sa_sigaction field, not sa_handler.
    action.sa_flags = SA_SIGINFO;

    // Set up the SIGPIPE signal handler to escalate to SIGABRT
    // in order to have the glog handler catch it and print all
    // of its lovely information.
    if (sigaction(SIGPIPE, &action, NULL) < 0) {
      PLOG(FATAL) << "Failed to set sigaction";
    }

    // We also do not want SIGTERM to dump a stacktrace, as this
    // can imply that we crashed, when we were in fact terminated
    // by user request.
    if (sigaction(SIGTERM, &action, NULL) < 0) {
      PLOG(FATAL) << "Failed to set sigaction";
    }
  }

  initialized->done();
}
Beispiel #15
0
void EventBase::SmoothLoopTime::setTimeInterval(uint64_t timeInterval) {
  expCoeff_ = -1.0/timeInterval;
  VLOG(11) << "expCoeff_ " << expCoeff_ << " " << __PRETTY_FUNCTION__;
}
Beispiel #16
0
void initialize(
    const string& _argv0,
    const Flags& flags,
    bool installFailureSignalHandler)
{
  static Once* initialized = new Once();

  if (initialized->once()) {
    return;
  }

  argv0 = _argv0;

  if (flags.logging_level != "INFO" &&
      flags.logging_level != "WARNING" &&
      flags.logging_level != "ERROR") {
    EXIT(EXIT_FAILURE)
      << "'" << flags.logging_level
      << "' is not a valid logging level. Possible values for"
      << " 'logging_level' flag are: 'INFO', 'WARNING', 'ERROR'.";
  }

  FLAGS_minloglevel = getLogSeverity(flags.logging_level);

  if (flags.log_dir.isSome()) {
    Try<Nothing> mkdir = os::mkdir(flags.log_dir.get());
    if (mkdir.isError()) {
      EXIT(EXIT_FAILURE)
        << "Could not initialize logging: Failed to create directory "
        << flags.log_dir.get() << ": " << mkdir.error();
    }
    FLAGS_log_dir = flags.log_dir.get();
    // Do not log to stderr instead of log files.
    FLAGS_logtostderr = false;
  } else {
    // Log to stderr instead of log files.
    FLAGS_logtostderr = true;
  }

  // Log everything to stderr IN ADDITION to log files unless
  // otherwise specified.
  if (flags.quiet) {
    FLAGS_stderrthreshold = 3; // FATAL.

    // FLAGS_stderrthreshold is ignored when logging to stderr instead
    // of log files. Setting the minimum log level gets around this issue.
    if (FLAGS_logtostderr) {
      FLAGS_minloglevel = 3; // FATAL.
    }
  } else {
    FLAGS_stderrthreshold = FLAGS_minloglevel;
  }

  FLAGS_logbufsecs = flags.logbufsecs;

#ifdef __linux__
  // Do not drop in-memory buffers of log contents. When set to true, this flag
  // can significantly slow down the master. The slow down is attributed to
  // several hundred `posix_fadvise(..., POSIX_FADV_DONTNEED)` calls per second
  // to advise the kernel to drop in-memory buffers related to log contents.
  // We set this flag to 'false' only if the corresponding environment variable
  // is not set.
  if (os::getenv("GLOG_drop_log_memory").isNone()) {
    FLAGS_drop_log_memory = false;
  }
#endif

  google::InitGoogleLogging(argv0.c_str());
  if (flags.log_dir.isSome()) {
    // Log this message in order to create the log file; this is because GLOG
    // creates the log file once the first log message occurs; also recreate
    // the file if it has been created on a previous run.
    LOG_AT_LEVEL(FLAGS_minloglevel)
      << google::GetLogSeverityName(FLAGS_minloglevel)
      << " level logging started!";
  }

  VLOG(1) << "Logging to " <<
    (flags.log_dir.isSome() ? flags.log_dir.get() : "STDERR");

  if (installFailureSignalHandler) {
    // glog on Windows does not support `InstallFailureSignalHandler`.
#ifndef __WINDOWS__
    // Handles SIGSEGV, SIGILL, SIGFPE, SIGABRT, SIGBUS, SIGTERM
    // by default.
    google::InstallFailureSignalHandler();

    // The code below sets the SIGTERM signal handler to the `handle` function
    // declared above. While this is useful on POSIX systems, SIGTERM is
    // generated and handled differently on Windows[1], so this code would
    // not work.
    // [1] https://msdn.microsoft.com/en-us/library/xdkz3x12.aspx

    // Set up our custom signal handlers.
    struct sigaction action;
    action.sa_sigaction = handler;

    // Do not block additional signals while in the handler.
    sigemptyset(&action.sa_mask);

    // The SA_SIGINFO flag tells sigaction() to use
    // the sa_sigaction field, not sa_handler.
    action.sa_flags = SA_SIGINFO;

    // We also do not want SIGTERM to dump a stacktrace, as this
    // can imply that we crashed, when we were in fact terminated
    // by user request.
    if (sigaction(SIGTERM, &action, nullptr) < 0) {
      PLOG(FATAL) << "Failed to set sigaction";
    }
#endif // __WINDOWS__
  }

  initialized->done();
}
Beispiel #17
0
bool EventBase::nothingHandledYet() {
  VLOG(11) << "latest " << latestLoopCnt_ << " next " << nextLoopCnt_;
  return (nextLoopCnt_ != latestLoopCnt_);
}
Beispiel #18
0
void VulkanRenderManager::CreateBackbuffers() {
	VkResult res = vkGetSwapchainImagesKHR(vulkan_->GetDevice(), vulkan_->GetSwapchain(), &swapchainImageCount_, nullptr);
	assert(res == VK_SUCCESS);

	VkImage *swapchainImages = new VkImage[swapchainImageCount_];
	res = vkGetSwapchainImagesKHR(vulkan_->GetDevice(), vulkan_->GetSwapchain(), &swapchainImageCount_, swapchainImages);
	if (res != VK_SUCCESS) {
		ELOG("vkGetSwapchainImagesKHR failed");
		delete[] swapchainImages;
		return;
	}

	VkCommandBuffer cmdInit = GetInitCmd();

	for (uint32_t i = 0; i < swapchainImageCount_; i++) {
		SwapchainImageData sc_buffer;
		sc_buffer.image = swapchainImages[i];

		VkImageViewCreateInfo color_image_view = { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO };
		color_image_view.format = vulkan_->GetSwapchainFormat();
		color_image_view.components.r = VK_COMPONENT_SWIZZLE_R;
		color_image_view.components.g = VK_COMPONENT_SWIZZLE_G;
		color_image_view.components.b = VK_COMPONENT_SWIZZLE_B;
		color_image_view.components.a = VK_COMPONENT_SWIZZLE_A;
		color_image_view.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
		color_image_view.subresourceRange.baseMipLevel = 0;
		color_image_view.subresourceRange.levelCount = 1;
		color_image_view.subresourceRange.baseArrayLayer = 0;
		color_image_view.subresourceRange.layerCount = 1;
		color_image_view.viewType = VK_IMAGE_VIEW_TYPE_2D;
		color_image_view.flags = 0;
		color_image_view.image = sc_buffer.image;

		// Pre-set them to PRESENT_SRC_KHR, as the first thing we do after acquiring
		// in image to render to will be to transition them away from that.
		TransitionImageLayout2(cmdInit, sc_buffer.image, 0, 1,
			VK_IMAGE_ASPECT_COLOR_BIT,
			VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
			VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
			0, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);

		res = vkCreateImageView(vulkan_->GetDevice(), &color_image_view, nullptr, &sc_buffer.view);
		swapchainImages_.push_back(sc_buffer);
		assert(res == VK_SUCCESS);
	}
	delete[] swapchainImages;

	// Must be before InitBackbufferRenderPass.
	if (InitDepthStencilBuffer(cmdInit)) {
		InitBackbufferFramebuffers(vulkan_->GetBackbufferWidth(), vulkan_->GetBackbufferHeight());
	}
	curWidth_ = -1;
	curHeight_ = -1;

	if (HasBackbuffers()) {
		VLOG("Backbuffers Created");
	}

	// Start the thread.
	if (useThread_ && HasBackbuffers()) {
		run_ = true;
		// Won't necessarily be 0.
		threadInitFrame_ = vulkan_->GetCurFrame();
		ILOG("Starting Vulkan submission thread (threadInitFrame_ = %d)", vulkan_->GetCurFrame());
		thread_ = std::thread(&VulkanRenderManager::ThreadFunc, this);
	}
}
void FrameTransport::onError(folly::exception_wrapper ex) noexcept {
  VLOG(6) << "onError" << ex.what();
  terminateFrameProcessor(std::move(ex));
}
Beispiel #20
0
Status EventFactory::registerEventSubscriber(const PluginRef& sub) {
  // Try to downcast the plugin to an event subscriber.
  EventSubscriberRef specialized_sub;
  try {
    auto base_sub = std::dynamic_pointer_cast<EventSubscriberPlugin>(sub);
    specialized_sub = std::static_pointer_cast<BaseEventSubscriber>(base_sub);
  } catch (const std::bad_cast& /* e */) {
    return Status(1, "Incorrect plugin");
  }

  if (specialized_sub == nullptr || specialized_sub.get() == nullptr) {
    return Status(1, "Invalid subscriber");
  }

  // The config may use an "events" key to explicitly enabled or disable
  // event subscribers. See EventSubscriber::disable.
  auto name = specialized_sub->getName();
  auto plugin = Config::getInstance().getParser("events");
  if (plugin != nullptr && plugin.get() != nullptr) {
    const auto& data = plugin->getData();
    // First perform explicit enabling.
    if (data.get_child("events").count("enable_subscribers") > 0) {
      for (const auto& item : data.get_child("events.enable_subscribers")) {
        if (item.second.data() == name) {
          VLOG(1) << "Enabling event subscriber: " << name;
          specialized_sub->disabled = false;
        }
      }
    }
    // Then use explicit disabling as an ultimate override.
    if (data.get_child("events").count("disable_subscribers") > 0) {
      for (const auto& item : data.get_child("events.disable_subscribers")) {
        if (item.second.data() == name) {
          VLOG(1) << "Disabling event subscriber: " << name;
          specialized_sub->disabled = true;
        }
      }
    }
  }

  // Let the module initialize any Subscriptions.
  auto status = Status(0, "OK");
  if (!FLAGS_disable_events && !specialized_sub->disabled) {
    specialized_sub->expireCheck(true);
    status = specialized_sub->init();
    specialized_sub->state(SUBSCRIBER_RUNNING);
  } else {
    specialized_sub->state(SUBSCRIBER_PAUSED);
  }

  auto& ef = EventFactory::getInstance();
  ef.event_subs_[name] = specialized_sub;

  // Restore optimize times for a daemon.
  if (kToolType == ToolType::DAEMON && FLAGS_events_optimize) {
    auto index_key = "optimize." + specialized_sub->dbNamespace();
    std::string content;
    if (getDatabaseValue(kEvents, index_key, content)) {
      long long optimize_time = 0;
      safeStrtoll(content, 10, optimize_time);
      specialized_sub->optimize_time_ = static_cast<EventTime>(optimize_time);
    }

    index_key = "optimize_id." + specialized_sub->dbNamespace();
    if (getDatabaseValue(kEvents, index_key, content)) {
      long long optimize_id = 0;
      safeStrtoll(content, 10, optimize_id);
      specialized_sub->optimize_eid_ = static_cast<size_t>(optimize_id);
    }
  }

  // Set state of subscriber.
  if (!status.ok()) {
    specialized_sub->state(SUBSCRIBER_FAILED);
    return Status(1, status.getMessage());
  } else {
    return Status(0, "OK");
  }
}
void FrameTransport::cancel() noexcept {
  VLOG(6) << "cancel";
  terminateFrameProcessor(folly::exception_wrapper());
}
bool EventBase::loopBody(int flags) {
  VLOG(5) << "EventBase(): Starting loop.";

  DCHECK(!invokingLoop_)
      << "Your code just tried to loop over an event base from inside another "
      << "event base loop. Since libevent is not reentrant, this leads to "
      << "undefined behavior in opt builds. Please fix immediately. For the "
      << "common case of an inner function that needs to do some synchronous "
      << "computation on an event-base, replace getEventBase() by a new, "
      << "stack-allocated EvenBase.";
  invokingLoop_ = true;
  SCOPE_EXIT {
    invokingLoop_ = false;
  };

  int res = 0;
  bool ranLoopCallbacks;
  bool blocking = !(flags & EVLOOP_NONBLOCK);
  bool once = (flags & EVLOOP_ONCE);

  // time-measurement variables.
  std::chrono::steady_clock::time_point prev;
  int64_t idleStart = 0;
  int64_t busy;
  int64_t idle;

  loopThread_.store(pthread_self(), std::memory_order_release);

  if (!name_.empty()) {
    setThreadName(name_);
  }

  if (enableTimeMeasurement_) {
    prev = std::chrono::steady_clock::now();
    idleStart = std::chrono::duration_cast<std::chrono::microseconds>(
      std::chrono::steady_clock::now().time_since_epoch()).count();
  }

  while (!stop_.load(std::memory_order_acquire)) {
    applyLoopKeepAlive();
    ++nextLoopCnt_;

    // Run the before loop callbacks
    LoopCallbackList callbacks;
    callbacks.swap(runBeforeLoopCallbacks_);

    while(!callbacks.empty()) {
      auto* item = &callbacks.front();
      callbacks.pop_front();
      item->runLoopCallback();
    }

    // nobody can add loop callbacks from within this thread if
    // we don't have to handle anything to start with...
    if (blocking && loopCallbacks_.empty()) {
      res = event_base_loop(evb_, EVLOOP_ONCE);
    } else {
      res = event_base_loop(evb_, EVLOOP_ONCE | EVLOOP_NONBLOCK);
    }

    ranLoopCallbacks = runLoopCallbacks();

    if (enableTimeMeasurement_) {
      busy = std::chrono::duration_cast<std::chrono::microseconds>(
        std::chrono::steady_clock::now().time_since_epoch()).count() -
        startWork_;
      idle = startWork_ - idleStart;

      avgLoopTime_.addSample(idle, busy);
      maxLatencyLoopTime_.addSample(idle, busy);

      if (observer_) {
        if (observerSampleCount_++ == observer_->getSampleRate()) {
          observerSampleCount_ = 0;
          observer_->loopSample(busy, idle);
        }
      }

      VLOG(11) << "EventBase "  << this         << " did not timeout "
        " loop time guess: "    << busy + idle  <<
        " idle time: "          << idle         <<
        " busy time: "          << busy         <<
        " avgLoopTime: "        << avgLoopTime_.get() <<
        " maxLatencyLoopTime: " << maxLatencyLoopTime_.get() <<
        " maxLatency_: "        << maxLatency_ <<
        " notificationQueueSize: " << getNotificationQueueSize() <<
        " nothingHandledYet(): "<< nothingHandledYet();

      // see if our average loop time has exceeded our limit
      if ((maxLatency_ > 0) &&
          (maxLatencyLoopTime_.get() > double(maxLatency_))) {
        maxLatencyCob_();
        // back off temporarily -- don't keep spamming maxLatencyCob_
        // if we're only a bit over the limit
        maxLatencyLoopTime_.dampen(0.9);
      }

      // Our loop run did real work; reset the idle timer
      idleStart = std::chrono::duration_cast<std::chrono::microseconds>(
        std::chrono::steady_clock::now().time_since_epoch()).count();
    } else {
      VLOG(11) << "EventBase " << this << " did not timeout";
    }

    // If the event loop indicate that there were no more events, and
    // we also didn't have any loop callbacks to run, there is nothing left to
    // do.
    if (res != 0 && !ranLoopCallbacks) {
      // Since Notification Queue is marked 'internal' some events may not have
      // run.  Run them manually if so, and continue looping.
      //
      if (getNotificationQueueSize() > 0) {
        fnRunner_->handlerReady(0);
      } else {
        break;
      }
    }

    if (enableTimeMeasurement_) {
      VLOG(5) << "EventBase " << this << " loop time: " <<
        getTimeDelta(&prev).count();
    }

    if (once) {
      break;
    }
  }
  // Reset stop_ so loop() can be called again
  stop_ = false;

  if (res < 0) {
    LOG(ERROR) << "EventBase: -- error in event loop, res = " << res;
    return false;
  } else if (res == 1) {
    VLOG(5) << "EventBase: ran out of events (exiting loop)!";
  } else if (res > 1) {
    LOG(ERROR) << "EventBase: unknown event loop result = " << res;
    return false;
  }

  loopThread_.store({}, std::memory_order_release);

  VLOG(5) << "EventBase(): Done with loop.";
  return true;
}
Beispiel #23
0
BOOL Config_ReadInt(const char *itemname, const char *desc, int def_value, int create, int isBoolean) {
    VLOG("Getting value %s", itemname);
    int z = INI_ReadInt(itemname, def_value, FALSE);
    if(isBoolean) z=(z && 1);
    return z;
}
Beispiel #24
0
QueryData genYara(QueryContext& context) {
  QueryData results;

  auto paths = context.constraints["path"].getAll(EQUALS);
  auto patterns = context.constraints["pattern"].getAll(EQUALS);
  auto groups = context.constraints["sig_group"].getAll(EQUALS);
  auto sigfiles = context.constraints["sigfile"].getAll(EQUALS);

  // Must specify a path constraint and at least one of sig_group or sigfile.
  if (groups.size() == 0 && sigfiles.size() == 0) {
    return results;
  }

  // XXX: Abstract this into a common "get rules for group" function.
  auto parser = Config::getParser("yara");
  if (parser == nullptr || parser.get() == nullptr) {
    LOG(ERROR) << "YARA config parser plugin has no pointer";
    return results;
  }

  std::shared_ptr<YARAConfigParserPlugin> yaraParser;
  try {
    yaraParser = std::dynamic_pointer_cast<YARAConfigParserPlugin>(parser);
  } catch (const std::bad_cast& e) {
    LOG(ERROR) << "Error casting yara config parser plugin";
    return results;
  }
  if (yaraParser == nullptr || yaraParser.get() == nullptr) {
    LOG(ERROR) << "YARA config parser plugin has no pointer";
    return results;
  }

  auto& rules = yaraParser->rules();

  // Store resolved paths in a vector of pairs.
  // Each pair has the first element as the path to scan and the second
  // element as the pattern which generated it.
  std::vector<std::pair<std::string, std::string> > path_pairs;

  // Expand patterns and push onto path_pairs.
  for (const auto& pattern : patterns) {
    std::vector<std::string> expanded_patterns;
    auto status = resolveFilePattern(pattern, expanded_patterns);
    if (!status.ok()) {
      VLOG(1) << "Could not expand pattern properly: " << status.toString();
      return results;
    }

    for (const auto& resolved : expanded_patterns) {
      if (!isReadable(resolved)) {
        continue;
      }
      path_pairs.push_back(make_pair(resolved, pattern));
    }
  }

  // Collect all paths specified too.
  for (const auto& path_string : paths) {
    if (!isReadable(path_string)) {
      continue;
    }
    path_pairs.push_back(make_pair(path_string, ""));
  }

  // Compile all sigfiles into a map.
  for (const auto& file : sigfiles) {
    // Check if this on-demand sigfile has not been used/compiled.
    if (rules.count(file) == 0) {
      auto path = (file[0] != '/') ? std::string("/etc/osquery/yara/") : "";
      path += file;

      YR_RULES* tmp_rules = nullptr;
      auto status = compileSingleFile(path, &tmp_rules);
      if (!status.ok()) {
        VLOG(1) << "YARA error: " << status.toString();
      } else {
        rules[file] = tmp_rules;
        groups.insert(file);
      }
    }
  }

  // Scan every path pair.
  for (const auto& path_pair : path_pairs) {
    // Scan using siggroups.
    for (const auto& group : groups) {
      if (rules.count(group) == 0) {
        continue;
      }

      VLOG(1) << "Scanning with group: " << group;
      auto status = doYARAScan(rules[group],
                               path_pair.first.c_str(),
                               path_pair.second,
                               results,
                               group,
                               group);
      if (!status.ok()) {
        VLOG(1) << "YARA error: " << status.toString();
      }
    }
  }

  return results;
}
Beispiel #25
0
 void deleteClient(const Message& m, HBClients& clients, HBMap& lastHeartbeats)
 {
   VLOG(1) << " HB system received GOODBYE from " << m.address.back();
   clients.erase(clients.find(m.address.back()));
   lastHeartbeats.erase(m.address.back());
 }
int
TLSTicketKeyManager::processTicket(SSL* ssl, unsigned char* keyName,
                                   unsigned char* iv,
                                   EVP_CIPHER_CTX* cipherCtx,
                                   HMAC_CTX* hmacCtx, int encrypt) {
  uint8_t salt[kTLSTicketKeySaltLen];
  uint8_t* saltptr = nullptr;
  uint8_t output[SHA256_DIGEST_LENGTH];
  uint8_t* hmacKey = nullptr;
  uint8_t* aesKey = nullptr;
  TLSTicketKeySource* key = nullptr;
  int result = 0;

  if (encrypt) {
    key = findEncryptionKey();
    if (key == nullptr) {
      // no keys available to encrypt
      VLOG(2) << "No TLS ticket key found";
      return -1;
    }
    VLOG(4) << "Encrypting new ticket with key name=" <<
      SSLUtil::hexlify(key->keyName_);

    // Get a random salt and write out key name
    RAND_pseudo_bytes(salt, (int)sizeof(salt));
    memcpy(keyName, key->keyName_.data(), kTLSTicketKeyNameLen);
    memcpy(keyName + kTLSTicketKeyNameLen, salt, kTLSTicketKeySaltLen);

    // Create the unique keys by hashing with the salt
    makeUniqueKeys(key->keySource_, sizeof(key->keySource_), salt, output);
    // This relies on the fact that SHA256 has 32 bytes of output
    // and that AES-128 keys are 16 bytes
    hmacKey = output;
    aesKey = output + SHA256_DIGEST_LENGTH / 2;

    // Initialize iv and cipher/mac CTX
    RAND_pseudo_bytes(iv, AES_BLOCK_SIZE);
    HMAC_Init_ex(hmacCtx, hmacKey, SHA256_DIGEST_LENGTH / 2,
                 EVP_sha256(), nullptr);
    EVP_EncryptInit_ex(cipherCtx, EVP_aes_128_cbc(), nullptr, aesKey, iv);

    result = 1;
  } else {
    key = findDecryptionKey(keyName);
    if (key == nullptr) {
      // no ticket found for decryption - will issue a new ticket
      if (VLOG_IS_ON(4)) {
        string skeyName((char *)keyName, kTLSTicketKeyNameLen);
        VLOG(4) << "Can't find ticket key with name=" <<
          SSLUtil::hexlify(skeyName)<< ", will generate new ticket";
      }

      result = 0;
    } else {
      VLOG(4) << "Decrypting ticket with key name=" <<
        SSLUtil::hexlify(key->keyName_);

      // Reconstruct the unique key via the salt
      saltptr = keyName + kTLSTicketKeyNameLen;
      makeUniqueKeys(key->keySource_, sizeof(key->keySource_), saltptr, output);
      hmacKey = output;
      aesKey = output + SHA256_DIGEST_LENGTH / 2;

      // Initialize cipher/mac CTX
      HMAC_Init_ex(hmacCtx, hmacKey, SHA256_DIGEST_LENGTH / 2,
                   EVP_sha256(), nullptr);
      EVP_DecryptInit_ex(cipherCtx, EVP_aes_128_cbc(), nullptr, aesKey, iv);

      result = 1;
    }
  }
  // result records whether a ticket key was found to decrypt this ticket,
  // not wether the session was re-used.
  if (stats_) {
    stats_->recordTLSTicket(encrypt, result);
  }

  return result;
}
Object *BlenderSync::sync_object(BL::Object b_parent, int persistent_id[OBJECT_PERSISTENT_ID_SIZE], BL::DupliObject b_dupli_ob,
                                 Transform& tfm, uint layer_flag, float motion_time, bool hide_tris, bool *use_portal)
{
    BL::Object b_ob = (b_dupli_ob ? b_dupli_ob.object() : b_parent);
    bool motion = motion_time != 0.0f;

    /* light is handled separately */
    if(object_is_light(b_ob)) {
        /* don't use lamps for excluded layers used as mask layer */
        if(!motion && !((layer_flag & render_layer.holdout_layer) && (layer_flag & render_layer.exclude_layer)))
            sync_light(b_parent, persistent_id, b_ob, tfm, use_portal);

        return NULL;
    }

    /* only interested in object that we can create meshes from */
    if(!object_is_mesh(b_ob))
        return NULL;

    /* key to lookup object */
    ObjectKey key(b_parent, persistent_id, b_ob);
    Object *object;

    /* motion vector case */
    if(motion) {
        object = object_map.find(key);

        if(object && (scene->need_motion() == Scene::MOTION_PASS ||
                      object_use_motion(b_parent, b_ob)))
        {
            /* object transformation */
            if(tfm != object->tfm) {
                VLOG(1) << "Object " << b_ob.name() << " motion detected.";
                if(motion_time == -1.0f) {
                    object->motion.pre = tfm;
                    object->use_motion = true;
                }
                else if(motion_time == 1.0f) {
                    object->motion.post = tfm;
                    object->use_motion = true;
                }
            }

            /* mesh deformation */
            if(object->mesh)
                sync_mesh_motion(b_ob, object, motion_time);
        }

        return object;
    }

    /* test if we need to sync */
    bool object_updated = false;

    if(object_map.sync(&object, b_ob, b_parent, key))
        object_updated = true;

    bool use_holdout = (layer_flag & render_layer.holdout_layer) != 0;

    /* mesh sync */
    object->mesh = sync_mesh(b_ob, object_updated, hide_tris);

    /* special case not tracked by object update flags */

    /* holdout */
    if(use_holdout != object->use_holdout) {
        object->use_holdout = use_holdout;
        scene->object_manager->tag_update(scene);
        object_updated = true;
    }

    /* visibility flags for both parent and child */
    uint visibility = object_ray_visibility(b_ob) & PATH_RAY_ALL_VISIBILITY;
    if(b_parent.ptr.data != b_ob.ptr.data) {
        visibility &= object_ray_visibility(b_parent);
    }

    /* make holdout objects on excluded layer invisible for non-camera rays */
    if(use_holdout && (layer_flag & render_layer.exclude_layer))
        visibility &= ~(PATH_RAY_ALL_VISIBILITY - PATH_RAY_CAMERA);

    /* camera flag is not actually used, instead is tested against render layer
     * flags */
    if(visibility & PATH_RAY_CAMERA) {
        visibility |= layer_flag << PATH_RAY_LAYER_SHIFT;
        visibility &= ~PATH_RAY_CAMERA;
    }

    if(visibility != object->visibility) {
        object->visibility = visibility;
        object_updated = true;
    }

    /* object sync
     * transform comparison should not be needed, but duplis don't work perfect
     * in the depsgraph and may not signal changes, so this is a workaround */
    if(object_updated || (object->mesh && object->mesh->need_update) || tfm != object->tfm) {
        object->name = b_ob.name().c_str();
        object->pass_id = b_ob.pass_index();
        object->tfm = tfm;
        object->motion.pre = tfm;
        object->motion.post = tfm;
        object->use_motion = false;

        /* motion blur */
        if(scene->need_motion() == Scene::MOTION_BLUR && object->mesh) {
            Mesh *mesh = object->mesh;

            mesh->use_motion_blur = false;

            if(object_use_motion(b_parent, b_ob)) {
                if(object_use_deform_motion(b_parent, b_ob)) {
                    mesh->motion_steps = object_motion_steps(b_ob);
                    mesh->use_motion_blur = true;
                }

                vector<float> times = object->motion_times();
                foreach(float time, times)
                    motion_times.insert(time);
            }
        }
Beispiel #28
0
bool EventBase::loopBody(bool once) {
  VLOG(5) << "EventBase(): Starting loop.";
  int res = 0;
  bool ranLoopCallbacks;
  int nonBlocking;

  loopThread_.store(pthread_self(), std::memory_order_release);

  if (!name_.empty()) {
    setThreadName(name_);
  }

  auto prev = std::chrono::steady_clock::now();
  int64_t idleStart = std::chrono::duration_cast<std::chrono::microseconds>(
    std::chrono::steady_clock::now().time_since_epoch()).count();

  // TODO: Read stop_ atomically with an acquire barrier.
  while (!stop_) {
    ++nextLoopCnt_;

    // nobody can add loop callbacks from within this thread if
    // we don't have to handle anything to start with...
    nonBlocking = (loopCallbacks_.empty() ? 0 : EVLOOP_NONBLOCK);
    res = event_base_loop(evb_, EVLOOP_ONCE | nonBlocking);
    ranLoopCallbacks = runLoopCallbacks();

    int64_t busy = std::chrono::duration_cast<std::chrono::microseconds>(
      std::chrono::steady_clock::now().time_since_epoch()).count() - startWork_;
    int64_t idle = startWork_ - idleStart;

    avgLoopTime_.addSample(idle, busy);
    maxLatencyLoopTime_.addSample(idle, busy);

    if (observer_) {
      if (observerSampleCount_++ == observer_->getSampleRate()) {
        observerSampleCount_ = 0;
        observer_->loopSample(busy, idle);
      }
    }

    VLOG(11) << "EventBase " << this         << " did not timeout "
     " loop time guess: "    << busy + idle  <<
     " idle time: "          << idle         <<
     " busy time: "          << busy         <<
     " avgLoopTime: "        << avgLoopTime_.get() <<
     " maxLatencyLoopTime: " << maxLatencyLoopTime_.get() <<
     " maxLatency_: "        << maxLatency_ <<
     " nothingHandledYet(): "<< nothingHandledYet();

    // see if our average loop time has exceeded our limit
    if ((maxLatency_ > 0) &&
        (maxLatencyLoopTime_.get() > double(maxLatency_))) {
      maxLatencyCob_();
      // back off temporarily -- don't keep spamming maxLatencyCob_
      // if we're only a bit over the limit
      maxLatencyLoopTime_.dampen(0.9);
    }

    // Our loop run did real work; reset the idle timer
    idleStart = std::chrono::duration_cast<std::chrono::microseconds>(
      std::chrono::steady_clock::now().time_since_epoch()).count();

    // If the event loop indicate that there were no more events, and
    // we also didn't have any loop callbacks to run, there is nothing left to
    // do.
    if (res != 0 && !ranLoopCallbacks) {
      // Since Notification Queue is marked 'internal' some events may not have
      // run.  Run them manually if so, and continue looping.
      //
      if (getNotificationQueueSize() > 0) {
        fnRunner_->handlerReady(0);
      } else {
        break;
      }
    }

    VLOG(5) << "EventBase " << this << " loop time: " <<
      getTimeDelta(&prev).count();

    if (once) {
      break;
    }
  }
  // Reset stop_ so loop() can be called again
  stop_ = false;

  if (res < 0) {
    LOG(ERROR) << "EventBase: -- error in event loop, res = " << res;
    return false;
  } else if (res == 1) {
    VLOG(5) << "EventBase: ran out of events (exiting loop)!";
  } else if (res > 1) {
    LOG(ERROR) << "EventBase: unknown event loop result = " << res;
    return false;
  }

  loopThread_.store(0, std::memory_order_release);

  VLOG(5) << "EventBase(): Done with loop.";
  return true;
}
 void NetworkContainerCombiner::setFitness(const double& fitness)
 {
     VLOG(5) << "Setting fitness";
     combinerNetwork->setFitness(fitness);
 }
Beispiel #30
0
bool Van::recv(mltools::Message *msg, size_t *recvBytes) {
  size_t dataSize = 0;
  msg->clear_data();
  for (int i = 0;; ++i) {
    zmq_msg_t *zmsg = new zmq_msg_t;
    CHECK(zmq_msg_init(zmsg) == 0) << zmq_strerror(errno);
    while (true) {
      if (zmq_msg_recv(zmsg, receiver_, 0) != -1) {
        break;
      }
      if (errno == EINTR) {
        continue;
      }
      LOG(WARNING) << "failed to receive message, error : "
                   << zmq_strerror(errno);
      return false;
    }
    char *buf = CHECK_NOTNULL((char *)zmq_msg_data(zmsg));
    size_t size = zmq_msg_size(zmsg);
    dataSize += size;

    if (i == 0) {
      // first message is the identity of the sender
      msg->sender_ = std::string(buf, size);
      msg->recver_ = myNode_.id();
      zmq_msg_close(zmsg);
      delete zmsg;
    } else if (i == 1) {
      CHECK(msg->task_.ParseFromArray(buf, size))
          << "failed to parse string from " << msg->sender_ << ". this is "
          << myNode_.id() << size;
      if (isScheduler() && msg->task_.control() &&
          msg->task_.ctrl().cmd() == Control::REQUEST_APP) {
        // In the start time of every work, they will send such message to
        // scheduler to get the config. The scheduler will store the file
        // descriptor for performance monitoring.
        int val[64];
        size_t valLen = sizeof(val);
        std::memset(val, 0, sizeof(val));
        CHECK(!zmq_getsockopt(receiver_, ZMQ_FD, (char *)val, &valLen))
            << "Failed to get the file descriptor of " << msg->sender_
            << ". with error: " << zmq_strerror(errno);
        CHECK_EQ(valLen, 4);
        int fd = val[0];
        VLOG(1) << "node [" << msg->sender_ << "] is on file descriptor " << fd;
        Lock l(fdToNodeIdMu_);
        fdToNodeId_[fd] = msg->sender_;
      }
      zmq_msg_close(zmsg);
      delete zmsg;
    } else {
      // keys & values from sender's message
      DArray<char> data(buf, size, false);
      data.pointer().reset(buf, [zmsg](char *) {
        zmq_msg_close(zmsg);
        delete zmsg;
      });
      if (i == 2 && msg->task_.has_key()) {
        msg->key_ = data;
      } else {
        msg->value_.push_back(data);
      }
    }

    if (!zmq_msg_more(zmsg)) {
      CHECK_GT(i, 0);
      break;
    }
  }
  *recvBytes += dataSize;
  if (hostnames_[msg->sender_] == myNode_.hostname()) {
    receivedFromLocal_ += dataSize;
  } else {
    receivedFromOthers_ += dataSize;
  }
  VLOG(1) << "FROM : " << msg->sender_ << msg->ShortDebugString();
  return true;
}